Support Forums

Full Version: VB 2008 - Useful Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Hello everyone. I previously posted this tutorial on HF , but I recently left and joined SF. I thought that this may be useful to some beginners.


Make the computer talk

Code:
Dim sapi
sapi = CreateObject("sapi.spvoice")
sapi.Speak("TextHere")


Create a messagebox

Code:
MsgBox("TextHere")

Create A Message Box With A Yes + No Button

Code:
Dim Responce As Integer
Responce = MsgBox("Are You Sure", vbYesNo, "DELETE")
If Responce = vbYes Then
  MsgBox(" You Clicked Yes!")
Else
MsgBox("You Clicked No!")
End If

To change buttons,replace "vbyesno" with one of the following:

Code:
vbYesNoCancel
vbCritical
vbExcalmation
vbInformation
vbMsgBoxHelp
vbMsgBoxRight
vbOkOnly
vbRetryCancel
vbDefaultButton1
vbDefaultButton2
vbDefaultButton3
vbApplicationmodal
vbQuestion
vbOkCancel
vbAbortRetryIgnore

Start a process
Code:
Process.Start("notepad")
Process.Start("www.hackforum.net")

Kill a process

Code:
Dim RunningProcess As System.Diagnostics.Process = Process.GetProcessesByName("taskmgr.exe")(0)
RunningProcess.Kill()

Rename a file

Code:
My.Computer.FileSystem.RenameFile ("C:\Program Files\Mozilla Firefox\firefox.exe", "Anything.exe")

Delete a file

Code:
Dim FileToDelete As String
FileToDelete = "C:\File.exe"
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)

Copy a file

Code:
Dim FileToCopy As String
Dim NewCopy As String

FileToCopy = "C:\test.exe"
NewCopy = "C:\NewTest.txt"

If System.IO.File.Exists(FileToCopy) = True Then
System.IO.File.Copy(FileToCopy, NewCopy)

Delete a registry key

Code:
My.Computer.Registry.LocalMachine.DeleteSubKey("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SafeBoot")

Create a registry key

Code:
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
regKey.CreateSubKey("MyApp")
regKey.Close()

Over write a text file and then add text to it

Code:
Dim OverWrite As String = "C:\Documents and Settings\All Users\Desktop\Example.txt"
If System.IO.File.Exists(OverWrite) = True Then
Dim ObjWriter4 As New System.IO.StreamWriter(OverWrite)
ObjWriter4.Write("Text To Add")
ObjWriter4.Close()

Create an if statement (File Deletion)

Code:
Dim FileToDeleteWithAIfStatement As String
FileToDeleteWithAIfStatement = "C:\Windows\I386\regedit.exe"
If System.IO.File.Exists(FileToDeleteWithAIfStatement) = True The
System.IO.File.Delete(FileToDeleteWithAIfStatement)
End If

For loop

Code:
For MessageBox = 1 To 60
MsgBox(" This Will Loop Sixty Times!")

Next MessageBox

Hide your application

Code:
Me.Hide ()

Send an email with an attachment via SMTP Source

Code:
Dim message As System.Net.Mail.MailMessage

Dim smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)


Dim attach As System.Net.Mail.Attachment


message = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text)


If My.Computer.FileSystem.FileExists(txtAttachment.Text) Then message.Attachments.Add(attach)

smtp.EnableSsl = True


smtp.Credentials = New System.Net.NetworkCredential("yourUsername@yourmail.com", "password")

Try

smtp.Send(message)


MessageBox.Show("Well, the mail message appears to have been a success!", " Successful?", MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch exc As Net.Mail.SmtpException

MessageBox.Show(exc.StatusCode.ToString, " Something Happened?", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try


Visual Basic 2008 tutorials:

Link 1
Link2

The history of Visual Basic:

Link1
Link2

Do not quote post do to size.Thank you.
these are nice dude sure they will help people out.
Thanks a lot, i've read this on HF before. Very useful, well done. Smile
Thank you. I hope I will be able to use one of these unique functions in my upcoming application.
Oh! I knew about all of them except from killing a process. Thanks.
(12-10-2009, 05:39 AM)thanasis2028 Wrote: [ -> ]Oh! I knew about all of them except from killing a process. Thanks.

You mean "for" instead of "from" right? xD
Thanks for the codes mate. The process ones really helped me Smile
O_O..
Very useful..Thanks dude..
Very useful Tongue Maybe i use this e-mail one later ! Thank you
Nice codes for newbies! Thank you!
Pages: 1 2 3 4