How To Start Programming
Free Computer Programming Tutorials For Beginners
1. Introduction
This tutorial will quite simply show you how to download Visual Basic 2008. Visual
Basic 2008 is the application that is used in the early tutorials for VB.NET but a new
version has been released, Visual Basic 2010. Feel free to download Visual Basic
2010 instead as there is no real difference apart from the visual aspects. If you
would like to use Visual Basic 2008 instead, that is fine also.
2. Hello World
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show("Hello World", "Message Box Title")
End Sub
End Class
3. Variables
Public Class Form1
Dim messagecontent As String = "Variables are so cool"
Dim mynumber As Integer = 10
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show(mynumber)
End Sub
End Class
4. If Statemenets
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If TextBox1.Text = "hello" Then
Label1.Text = "world"
End If
End Sub
End Class
5. Math Functions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show(20 + 5)
MessageBox.Show(20 - 5)
MessageBox.Show(20 * 5)
MessageBox.Show(20 / 5)
End Sub
End Class
6. Form Properties
This tutorial will cover how to customize your form by changing the properties
of the object. No coding is involved as this is all visual.
7. Progress Bar
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ProgressBar1.Value = ProgressBar1.Value + 20
End Sub
End Class
8. List Box
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
End Sub
End Class
9. Radio Button And Check Box
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
MessageBox.Show("You are under 16!")
ElseIf RadioButton2.Checked = True Then
MessageBox.Show("You are over 16!")
End If
If CheckBox1.Checked = True Then
MessageBox.Show("You like Harry Potter, nerd")
End If
If CheckBox2.Checked = True Then
MessageBox.Show("What the hell, 2010 sucked")
End If
If CheckBox3.Checked = True Then
MessageBox.Show("the simpsons movie is a conspiracy dude")
End If
End Sub
End Class
10. Menu Strip
Public Class Form1
Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
MessageBox.Show("this is the help form")
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
End Class
11. Linking Forms
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub
End Class
12. Log In Form
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If TextBox1.Text = "password123" Then
data.Show()
Else
MessageBox.Show("Wrong Password!")
End If
End Sub
End Class
13. Text To Speech
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak("Welcome to the 13th visual basic .net tutorial")
End Sub
End Class
14. Splash Screen
This video tutorial will teach you how to add a Splash Screen to your
application. A Splash Screen is a customizable screen that will appear for a
few seconds as your application loads up. It is a very professional feature to
have in your application.
15. For Loop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer = 0
For i = 0 To 10
MessageBox.Show("The value of i is: " & i)
Next
End Sub
End Class
16. Do While
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim num1 As Integer = 1
Do While num1 < 10
MessageBox.Show("The value of num1 is :" & num1)
num1 = num1 + 1
Loop
End Sub
End Class
17. Do Until
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim num As Integer = 1
Do Until num = 5
MessageBox.Show("The value of num is: " & num)
num = num + 1
Loop
End Sub
End Class
18. Subs
Public Class Form1
Sub givemessage()
MessageBox.Show("I am cool for using subs")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call givemessage()
End Sub
End Class
19. Functions
Public Class Form1
Dim labeltext As String = "functions are cool lol"
Private Function changelabeltext()
Return labeltext
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Label1.Text = changelabeltext()
End Sub
End Class
20. Advanced MessageBox
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If MessageBox.Show("whatever", "whatever", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("you clicked yes")
Else
MessageBox.Show("you clicked no")
End If
End Sub
End Class
21. Conditional Operators
This tutorial will cover the conditional operators in VB.NET. Conditional
operators are used mainly in math equations to compare two different numbers
and then execute a set of code depending on if the condition is true or not.
For example, a conditional operator could be used to decide if 5 is a larger
number than 4. As the code constantly changes in this video tutorial, no
source code is included.
22. Logical Operators
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MessageBox.Show("You didn't fill in the fields!")
End If
If TextBox1.Text = "thepassword" And TextBox2.Text =
"thesecondpassword" Then
Form2.Show()
End If
End Sub
End Class
23. Timers
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
TextBox1.Text = TextBox1.Text + 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
End Class
24. Sending An Email
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Mail As New MailMessage
Mail.Subject = "test email"
Mail.To.Add("youremail@googlemail.com")
Mail.From = New MailAddress("youremail@googlemail.com")
Mail.Body = "This is an ownage email using VB.NET"
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("username",
"password")
SMTP.Port = "587"
SMTP.Send(Mail)
End Sub
End Class
25. Emailing Application
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Mail As New MailMessage
Mail.Subject = "test email"
Mail.To.Add(TextBox2.Text)
Mail.From = New MailAddress(TextBox2.Text)
Mail.Body = TextBox1.Text
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential(TextBox2.Text,
TextBox3.Text)
SMTP.Port = "587"
SMTP.Send(Mail)
End Sub
End Class
26. FTP Upload
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim request As System.Net.FtpWebRequest =
DirectCast(System.Net.WebRequest.Create("ftp://ftp.drivehq.com/file.txt"),
System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential("user",
"password")
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim file() As Byte = System.IO.File.ReadAllBytes("c:\file.txt")
Dim strz As System.IO.Stream = request.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
End Sub
End Class
27. FTP Application
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim request As System.Net.FtpWebRequest =
DirectCast(System.Net.WebRequest.Create("ftp://ftp.drivehq.com/file.txt"),
System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential(TextBox1.Text,
TextBox2.Text)
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim file() As Byte = System.IO.File.ReadAllBytes(TextBox3.Text)
Dim strz As System.IO.Stream = request.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
End Sub
End Class
28. ByVal And ByRef
This tutorial will teach you about ByVal and ByRef . If you want to create a
proper function, you need to learn how to use ByVal and ByRef. They will allow
you to create your own function with advanced features. The concept of ByVal
and ByRef is one that is very hard to grasp, so dont worry if you dont get
it the first time. As the code changes in this tutorial, no source code will
be provided.
29. Comments
Public Class Form1
'===============================================
'This button will message the user with "hello"=
'===============================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show("hello")
End Sub
End Class
30. Download And Run File
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
My.Computer.Network.DownloadFile("url", "C:\downloadedfile.exe")
Process.Start("C:\downloadedfile.exe")
End Sub
End Class
31. Web Browser
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
WebBrowser1.GoBack()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
WebBrowser1.GoForward()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
WebBrowser1.Refresh()
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles
WebBrowser1.DocumentCompleted
TextBox1.Text = WebBrowser1.Url.ToString
End Sub
End Class
32. Application Settings
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Label1.Text = My.Settings.greettext
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
My.Settings.greettext = TextBox1.Text
End Sub
End Clas
33. Using Settings
In the previous tutorial, we covered how to create and change application
settings. In this tutorial we look at some practical uses for application
settings.
34. Auto Typer
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
SendKeys.Send(TextBox1.Text)
SendKeys.Send("{Enter}")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Timer1.Interval = TextBox2.Text * 1000
Timer1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
End Class
35. Arrays
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tvshows(4) As String
tvshows(0) = "South Park"
tvshows(1) = "Family Guy"
tvshows(2) = "American Dad"
tvshows(3) = "The Simpsons"
tvshows(4) = "Supernatural"
For i = 0 To tvshows.Length - 1
MessageBox.Show(tvshows(i))
Next
End Sub
End Class
36. Splitting Strings
Dim somestring2() As String
domestring2 = TextBox1.Text.Split(TextBox2.Text)
For i = 0 to somestring2.Length - 1
MessageBox.Show(somestring2(i))
Next
37. Computer Information
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TextBox1.Text = My.Computer.Name
TextBox2.Text = My.User.Name
TextBox3.Text = My.Computer.Info.OSFullName
TextBox4.Text = My.Computer.Keyboard.CapsLock
TextBox5.Text = My.Computer.Mouse.WheelExists
TextBox6.Text = My.Computer.Screen.WorkingArea.ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
End Sub
End Class
38. Try Catch
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Process.Start(TextBox1.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
39. Web Page Source Code
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim request As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create(TextBox2.Text)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader(response.GetResponseStream())
Dim sourcecode As String = sr.ReadToEnd()
TextBox1.Text = sourcecode
End Sub
End Class
40. Application Updater
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim request As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create("http://h1.ripway.com/TeachMeComputer/current
version.txt")
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader(response.GetResponseStream())
Dim newestversion As String = sr.ReadToEnd()
Dim currentversion As String = Application.ProductVersion
If newestversion.Contains(currentversion) Then
MessageBox.Show("You have the current version")
Else
MessageBox.Show("Newer version available")
End If
End Sub
End Class
41. Website Login
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http://login.yahoo.com/")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
WebBrowser1.Document.GetElementById("login").SetAttribute("value",
TextBox1.Text)
WebBrowser1.Document.GetElementById("passwd").SetAttribute("value",
TextBox2.Text)
WebBrowser1.Document.GetElementById(".save").InvokeMember("click")
End Sub
End Class
42. Reading Text Files
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If System.IO.File.Exists(TextBox1.Text) = True Then
Dim objreader As New System.IO.StreamReader(TextBox1.Text)
TextBox2.Text = objreader.ReadToEnd
objreader.Close()
Else
MessageBox.Show("That file does not exist!")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox2.Clear()
End Sub
End Class
43. Saving Text Files
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If System.IO.File.Exists(TextBox1.Text) = True Then
MessageBox.Show("That file does not exist!")
Else
Dim objwriter As New System.IO.StreamWriter(TextBox1.Text)
objwriter.Write(TextBox2.Text)
objwriter.Close()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Process.Start(TextBox1.Text)
End Sub
End Class
44. OpenFileDialog
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e
As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
TextBox1.Text = OpenFileDialog1.FileName
End Sub
End Class
45. SaveFileDialog
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SaveFileDialog1.ShowDialog()
End Sub
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e
As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Dim FileToSaveAs As String = SaveFileDialog1.FileName
Dim objwriter As New System.IO.StreamWriter(FileToSaveAs)
objwriter.Write(TextBox1.Text)
objwriter.Close()
End Sub
End Class
46. Special Folders
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim thepath As String =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Process.Start(thepath & "\image.jpg")
End Sub
End Class
47. Get Elements By Tag Name
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles
WebBrowser1.DocumentCompleted
Dim PageElements As HtmlElementCollection =
WebBrowser1.Document.GetElementsByTagName("img")
For Each CurElement As HtmlElement In PageElements
TextBox2.Text = TextBox2.Text & CurElement.GetAttribute("src") &
Environment.NewLine
Next
End Sub
End Class
48. Get All HTML Elements
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("www.youtube.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles
WebBrowser1.DocumentCompleted
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
ListBox1.Items.Add(webpageelement.GetAttribute("src"))
Next
End Sub
End Class
49. Click Button Without ID
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://login.facebook.com/login.php")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("value") = "Log In" Then
webpageelement.InvokeMember("click")
End If
Next
End Sub
End Class
50. Write To Online Text File
Imports System.Net
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim request As WebRequest =
WebRequest.Create("http://h1.ripway.com/writetext/post.php?w=" &
TextBox1.Text)
request.GetResponse()
End Sub
End Class
51. HttpWebRequest POST Method
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
Dim logincookie As CookieContainer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim postData As String = "referer=http%3A%2F%2Fforums.zybez.net
%2Findex.php%3Fapp%3Dcore%26module%3Dglobal%26section%3Dlogin&username=" &
TextBox1.Text & "&password=" & TextBox2.Text & "&rememberMe=1"
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest =
DirectCast(WebRequest.Create("http://forums.zybez.net/index.php?
app=core&module=global§ion=login&do=process"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "http://forums.zybez.net/index.php?
app=core&module=global§ion=login&do=process"
postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru;
rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New
StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
WebBrowser1.DocumentText = RichTextBox1.Text
End Sub
End Class
52. HttpWebRequest CookieContainer
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
Dim logincookie As CookieContainer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim postData As String = "referer=http%3A%2F%2Fforums.zybez.net
%2Findex.php%3Fapp%3Dcore%26module%3Dglobal%26section%3Dlogin&username=" &
TextBox1.Text & "&password=" & TextBox2.Text & "&rememberMe=1"
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest =
DirectCast(WebRequest.Create("http://forums.zybez.net/index.php?
app=core&module=global§ion=login&do=process"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "http://forums.zybez.net/index.php?
app=core&module=global§ion=login&do=process"
postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru;
rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New
StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
WebBrowser1.DocumentText = RichTextBox1.Text
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
WebBrowser1.DocumentText = RichTextBox2.Text
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim request As HttpWebRequest =
DirectCast(WebRequest.Create("http://forums.zybez.net/index.php?
app=core&module=usercp"), HttpWebRequest)
request.CookieContainer = logincookie
Dim response As HttpWebResponse = DirectCast(request.GetResponse(),
HttpWebResponse)
Dim reader As New StreamReader(response.GetResponseStream())
Dim theusercp As String = reader.ReadToEnd
RichTextBox2.Text = theusercp
End Sub
End Class
53. MultiThreading
Public Class Form1
Dim i As Integer
Dim i2 As Integer
Dim thread As System.Threading.Thread
Dim thread2 As System.Threading.Thread
Private Sub countup()
Do Until i = 10000
i = i + 1
Label1.Text = i
Me.Refresh()
Loop
End Sub
Private Sub countup2()
Do Until i2 = 10000
i2 = i2 + 1
Label2.Text = i2
Me.Refresh()
Loop
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
thread = New System.Threading.Thread(AddressOf countup)
thread.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
thread2 = New System.Threading.Thread(AddressOf countup2)
thread2.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.CheckForIllegalCrossThreadCalls = False
End Sub
End Class
54. Regex
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim request As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create("http://services.runescape.com/m=itemdb_rs/fr
ontpage.ws")
Dim response As System.Net.HttpWebResponse = request.GetResponse
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader(response.GetResponseStream())
Dim rssourcecode As String = sr.ReadToEnd
Dim r As New System.Text.RegularExpressions.Regex("
")
Dim matches As MatchCollection = r.Matches(rssourcecode)
For Each itemcode As Match In matches
ListBox1.Items.Add(itemcode.Value.Split("""").GetValue(5))
Next
End Sub
End Class
55. Modules
Module Code
Module functions
Public bla As Integer = 10
Public bla2 As Integer = 20
Public Function addnumbers(ByVal number1 As Integer, ByVal number2 As
Integer)
Return number1 + number2
End Function
End Module
Code
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TextBox3.Text = addnumbers(bla, bla2)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
MessageBox.Show(addnumbers(TextBox1.Text, TextBox2.Text))
MessageBox.Show(functions.addnumbers(TextBox1.Text, TextBox2.Text))
End Sub
End Class
56. Classes
Class Code
Public Class person
Public
Public
Public
Public
firstname As String
lastname As String
eyecolour As String
haircolour As String
Public Sub setvalues(ByVal first As String, ByVal last As String, ByVal
eye As String, ByVal hair As String)
firstname = first
lastname = last
eyecolour = eye
haircolour = hair
End Sub
End Class
Code
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim bob As New person
bob.firstname = "bob"
bob.lastname = "smith"
bob.eyecolour = "brown"
bob.haircolour = "black"
bob.setvalues("bob", "smith", "brown", "black")
MessageBox.Show("Bobs hair colour is " & bob.haircolour)
End Sub
End Class