Support Forums
VB 2008 - Useful Code - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: VB 2008 - Useful Code (/showthread.php?tid=3199)

Pages: 1 2 3 4


VB 2008 - Useful Code - Apache - 11-28-2009

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.


RE: VB 2008 - Useful Code - Smed - 11-28-2009

these are nice dude sure they will help people out.


RE: VB 2008 - Useful Code - flAmingw0rm - 11-28-2009

Thanks a lot, i've read this on HF before. Very useful, well done. Smile


RE: VB 2008 - Useful Code - Red X - 12-04-2009

Thank you. I hope I will be able to use one of these unique functions in my upcoming application.


RE: VB 2008 - Useful Code - thanasis2028 - 12-10-2009

Oh! I knew about all of them except from killing a process. Thanks.


RE: VB 2008 - Useful Code - Red X - 12-10-2009

(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


RE: VB 2008 - Useful Code - HmanDude - 12-11-2009

Thanks for the codes mate. The process ones really helped me Smile


RE: VB 2008 - Useful Code - RaZoR03 - 04-18-2010

O_O..
Very useful..Thanks dude..


RE: VB 2008 - Useful Code - trubufo - 04-28-2010

Very useful Tongue Maybe i use this e-mail one later ! Thank you


RE: VB 2008 - Useful Code - TurB0 - 04-29-2010

Nice codes for newbies! Thank you!