Guide to VB.Net: Intermediate
Guide to VB.Net: Intermediate
This guide is for those who have already completed my beginner's guide and are ready to move onto more complicated things. This guide will deal with a larger range of commands within the program only.
Part 1: String Commands
Now we briefly discussed strings in the last tutorial. Strings are a very important part of making a successful VB.Net application. On a website like Logicalgamers, strings are inevitably essential as part of your application in order to connect to websites like Gaiaonline, Neopets, etc... So, with that being said let's begin.
A very useful string command that was added to VB.Net is the boolean "Contains". A boolean means, that it returns a true or false depending on the circumstances. This can be seen in the example below: Code:
Public Class Form1
Dim String1 As String = "I WENT TO THE LOL STORE"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If String1.Contains("LOL") Then
MsgBox("String1 contains the text: LOL")
Else
MsgBox("String1 does not contain any cool text")
End If
End Sub
End Class
This code simplifies something when declaring a variable. It declares the variable, then sets its value within the same line. Dim String1 as String = "LOL" will create a new string called String1 and its value will be LOL as opposed to a null value. Now this uses the If function to find out whether or not String1 contains the substring "LOL". If it does, it will create a message/popup box that says String1 contains the text: LOL. If not, it will say String1 does not contain any cool text.
You can also replace certain substrings within a string, with another string. This could be quite useful in simplifying strings, or changing tags, etc. An example is below: Code:
Public Class Form1
Dim String1 As String = "I WENT TO THE LOL STORE"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
String1 = String1.Replace(" ", "_")
End Sub
End Class
What this does, is replaces all single spaces in String1, with an underscore. It's quite simple and easy to use.
Another very useful string function is the split. What split will do is use a string array to split a string into two strings where you can define where to split it at. An example is: Code:
Public Class Form1
Dim String1 As String = "Username:Password"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim String2() As String = String1.Split(":")
MsgBox(String2(0) & " <-Split-> " & String2(1))
End Sub
End Class
In order to define an array, we need to put "()" at the end of the name of the object being declared. This is required for a split function to work. Now, in String1.Split(":"), this splits String1 by the colon ( : ). Then the messagebox displays the first and second substring separated by a <-Split-> so the final output would be Username <-Split-> Password.
The final and most useful string subject we will discuss is combining strings. I showed you in other tutorials and examples, but it wasn't the focus. It is now. In order to combine strings, there is one simple easy thing you have to do so find it below: Code:
Public Class Form1
Dim String1 As String = "ABC"
Dim String2 As String = "XYZ"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim String3 As String = String1 & String2
End Sub
End Class
This code will take String1 and String2 and combine them which ultimately equals ABCXYZ. You can also combine strings that are defined on the spot such as Dim String3 as String = String1 & ":" & String2 (which would put String1, then a colon, then String2).
Part 2: Application Settings
Don't you hate it when you download an application and it doesn't save user settings for a save file location? Well we can fix that in our own applications by using the user settings page. To get to this page, we must first open our project properties and navigate to the Settings tab as shown here. As you can see, we're selecting the name cell. Create a new cell by typing in when selecting the name so we'll name our setting Textbox1Contents. In order for these settings to save, we need something for it to save so place a textbox in your form named textbox1. Now at this point, our code should look like this in order to load the settings: Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.Textbox1Contents
End Sub
End Class
This will load the settings Textbox1Contents into Textbox1 when the form loads. Next we need the settings to change when the user edits the textbox, in order to do this our code will look like this: Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.Textbox1Contents
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
My.Settings.Textbox1Contents = TextBox1.Text
End Sub
End Class
This will save the Textbox1 text into Textbox1Contents when the text in Textbox1 is changed. Finally, we need to save these changes when the form is closing so our final code will look like this: Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.Textbox1Contents
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
My.Settings.Textbox1Contents = TextBox1.Text
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
My.Settings.Save()
End Sub
End Class
Now, debug your project. Enter anything into the textbox, then stop debugging. Start debugging again and see what happens.
Part 3: Essential Statements
Your application will never be worth anything without these statements in them. These are just as essential as strings if not more than strings. We will start off with the If Then statement. The If then can look like this: Code:
Public Class Form1
Dim Integer1 As Integer = 2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Integer1 = 0 Then
MsgBox("Nothing")
ElseIf Integer1 = 1 Then
MsgBox("Single")
ElseIf Integer1 = 2 Then
MsgBox("Double")
Else
MsgBox("Out of Bounds")
End If
End Sub
End Class
Now if we examine it, we will definitely see something in the organization. You start out with If <Condition> <Boolean> Then; then the next like would be the code if the condition is met, if you want something else to happen if another condition is met, then you would put under the If's code, ElseIf <Condition> <Boolean> and you can use ElseIf as much as you want, but If can be only used once. If none of the conditions our met, you can set something to happen, or not set anything at all. If you do want to set something to happen if no conditions are met, you would put Else and then your code. You also need to end with End If to signal that the current If has stopped.
A very easy to use statement is the Do Loop statement. What this will do is very self explanatory. View the code below: Code:
Public Class Form1
Dim Integer1 As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Do
MsgBox(Integer1)
Integer1 += 1
Loop
End Sub
End Class
This code will send a messagebox of the number value of Integer1, then it will add 1 to Integer1 and at Loop, it goes back to Do and repeats for an infinite number of times. There are ways to change the conditions for when it loops such as Do Until <Condition> <Boolean> or Do While <Condition> <Boolean>.
Another important statement is For and Next. This was showed in an example earlier and was only discussed briefly. The For and Next is shown below: Code:
Public Class Form1
Dim Integer1 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Integer1 = 1 To 5
MsgBox(Integer1)
Next
End Sub
End Class
This code may not seem like much anything until I explain what it does. This code will count from 1 to 5 and every time the count increases, the statement (Msgbox(Integer1)) is executed. In this case, every time Integer1 increases, the number is displayed in a messagebox. To simplify further, it really only works with numerical values, and loops at the Next.
Part 4: Lesson Test
Create your own program now. The program needs 1 string, and 2 integers. 1 Integer will increase each time it loops. Whenever it increases, it will do more commands with integers within it and combine it with a string. So simplified, it will have the template of: Code:
Public Class Form1
Private Sub LessonTest()
Do
For
String = String & Integer
Next
Loop
End Sub
End Class
Everything in bold/red is what you need to make work correctly. I suggest examining the code a lot better to see what exactly is happening in this sequence.
◄ Previous