Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VB 2008 - Useful Code
#1
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.
Reply
#2
these are nice dude sure they will help people out.
Reply
#3
Thanks a lot, i've read this on HF before. Very useful, well done. Smile
Reply
#4
Thank you. I hope I will be able to use one of these unique functions in my upcoming application.
[Image: ouwg0slq0vtagvzl4u8w.jpg]

Giveaway of the Day: [Image: ticker1.png]
Reply
#5
Oh! I knew about all of them except from killing a process. Thanks.
My software company: Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber
Reply
#6
(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
[Image: ouwg0slq0vtagvzl4u8w.jpg]

Giveaway of the Day: [Image: ticker1.png]
Reply
#7
Thanks for the codes mate. The process ones really helped me Smile
Reply
#8
O_O..
Very useful..Thanks dude..
[Image: 20r9vh4.jpg]
Reply
#9
Very useful Tongue Maybe i use this e-mail one later ! Thank you
Reply
#10
Nice codes for newbies! Thank you!
Returning to SF / HF. Long story Tongue
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get Country Code In 1 Line Of Code. wchar_t 13 4,641 11-28-2012, 06:49 AM
Last Post: Cubs
  [Pandora Incorporated] Visual Basic 2008 - Source Code Grabber [VIDEO TUTORIAL] Marik™ 13 4,666 05-23-2011, 06:41 PM
Last Post: KoBE
  [Pandora Incorporated]Visual Basic 2008 -MD5 Encryptor [VIDEO TUTORIAL] Marik™ 2 1,658 05-13-2011, 11:06 AM
Last Post: Imports System.Net
  [Pandora Incorporated] Visual Basic 2008 - Text To Speech [VIDEO TUTORIAL] Marik™ 1 1,572 04-14-2011, 05:37 PM
Last Post: Emily
  [Help] [VB.NET 2008] Textbox Contents Encryption. Example 4 4,001 02-23-2011, 01:16 AM
Last Post: Example

Forum Jump:


Users browsing this thread: 1 Guest(s)