A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Guide to VB.Net: Advanced

1.4k views · started by TEMPTii ·
#1
Guide to VB.Net: Advanced
Guide to VB.Net: Advanced
Viewing this guide means that you have viewed the intermediate tutorial. If you have not, go take a look at it. In this tutorial, you will learn how to extend upon what you already know to the point where it can be practically used in a useful application.

Part 1: Operators
Now, you probably already have recognized the standard operator of addition (+) but what are all the others? These operators will be essential to number based functions which you will need to create useful programs. The most common operators are listed below:
  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
These are the most common for numerical operations. An example of there use is shown below:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intDegreesF As Integer = 70
Dim intDegreesC As Integer
intDegreesC = (9 / 5) * intDegreesF + 32
End Sub
End Class
This simple code converts Fahrenheit into Celsius.

There are also inequality operators that we will need to know, these will be essential as well. Below are the most common inequality operators:
  • = Equals
  • <> Not Equal
  • < Less Than
  • > Greater Than
  • >= Greater Than or Equal To
  • <= Less Than or Equal To
An example of these in use is shown below:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 3
If i > 5 Then
MsgBox("I is greater than 5")
ElseIf i <= 2 Then
MsgBox("I is less than (or is) 2")
ElseIf i = 6 Then
MsgBox("I is 6")
End If
End Sub
End Class


Part 2: Do Loop
I briefly explained the Do/Loop function in the last tutorial. Whatever is contained within the first line Do, and the final line Loop, will be repeated forever but sometimes forever isn't best. In order to Loop until a specific criteria is met, we can use the While or Until methods. The code below should explain:
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
Do While i <= 10
MsgBox(i)
i = i + 1
Loop
End Sub
End Class
This will only execute the commands while i is less than or equal to 10. If i is greater than 10 then it will continue the sub beyond the Loop. This code can also be replicated using the Do Until (my preference) method as shown below:
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
Do Until i = 11
MsgBox(i)
i = i + 1
Loop
End Sub
End Class
In this code, once i is equal to 11, it will continue the sub beyond the Loop.

Part 3: Accessing Data
In many cases, you will be in a situation where you will have multiple forms and you will need to access the data or even share the data amongst the forms. This can be done in different ways which are very simple.

The easy way into doing this is simply by adding a small piece of text to your code. Let's say you want to change the textbox in form2's text to something else. In order to do this, we could simple access it as shown below:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CString As String = "String Changed"
Form2.Textbox1.Text = CString
End Sub
End Class
See? The only difference was adding Form2. in front of the textbox's name. You can read and write data to external forms easily like this. Now, the more pro way to go about doing this is by using a module. To get a module, go to Project>Add Windows Form>Module and a new module will be created. The only requirement for a module's data to be accessed is by declaring it using Public. An example is shown below:
Module Module1
Public String1 As String
End Module
Now, in any form, String1 can be referred to without any precedents. For example:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
String1 = "k"
End Sub
End Class
The application already knows that we're talking about Module1.String1 since its been declared using Public method.

Part 4: Imports
For some odd reason, Microsoft thought it was a good idea to not automatically include everything in your project, so you have to Imports things to accomplish certain tasks. This is very simple, all you need to put is Imports and a Namespace to import into the project. Shown below is importing the Threading namespace. Without this threading imported, the Sleep code would not work and would result in an error.
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.Sleep(5000)
End Sub
End Class

It's just that simple, for programmers at this level, you will basically only need System.IO, System.Threading, and possibly System.Text if you're that good.

Part 5: e as Arguments
In most subs, there is all that crap you need in order to handle a function such as Button1 being clicked. If you look closely, e is always the constant declaration in them but what does it do? Lets test it out with a Web Browser control.
Public Class Form1
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
MsgBox(e.Url.AbsoluteUri)
End Sub
End Class
As you can see, e is a variable and has it's own little properties. After pressing the period, the list showed that URL was my only choice, so that's what I entered. Then, there were even more properties of that property and I chose AbsoluteURI. Now, what is returned after this if my browser is set to view Google on form loadup? Copy the code and find out. If you did it correctly, it would write the page title, and the URI. If you simply explore all of the declarations' arguments, you can find that it will be very useful in all your applications.

Part 6: Lesson Test
Now, your lesson test for this will be to put together an application that uses a lot of numerical functions. Make a Do/Loop that will increase the integer declared by any amount each time, and every time it's looped, make it complete a formula. Create a messagebox of the result of that formula and have the thread sleep for 2 seconds until the loop. Loop 5 times and then stop.


◄ Previous
#2
the formula for fahrenheit to celsius is (F-32)*5/9
what you put is celsius to fahrenhiet (70 degrees celsius = 158 degrees fahrenheit) but you labeled them incorrectly :]
also, in this
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 3
If i > 5 Then
MsgBox("I is greater than 5")
ElseIf i <= 2 Then
MsgBox("I is less than (or is) 2")
ElseIf i = 6 Then
MsgBox("I is 6")
End If
End Sub
End Class

i=3, and none of your ElseIfs say i = 3 or anything related. just less than 2/greater than five/6
#3
lilvoice32 wrote:
the formula for fahrenheit to celsius is (F-32)*5/9
what you put is celsius to fahrenhiet (70 degrees celsius = 158 degrees fahrenheit) but you labeled them incorrectly :]
also, in this
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 3
If i > 5 Then
MsgBox("I is greater than 5")
ElseIf i <= 2 Then
MsgBox("I is less than (or is) 2")
ElseIf i = 6 Then
MsgBox("I is 6")
End If
End Sub
End Class

i=3, and none of your ElseIfs say i = 3 or anything related. just less than 2/greater than five/6


I don't care if it was the real equation or not, it was an example. Google's a bitch.

Then change i... if you're in the advanced that shouldn't be a problem <thumbs up>. i should be defined by a numeric updown control anyways.
#4
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
Do While i <= 10
MsgBox(i)
i = i + 1
Loop
End Sub
End Class

so how would i be able to turn this into a progress bar, cuz i tried just changing "i", into textbox2 and didnt get anything :\ or am i suppose to put this in a different sub.?
#5
You said you wanted it to have an ability to loop for a certain amount of times. To do so, it would be:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 0
ProgressBar1.Maximum = TextBox1.Text
Do Until ProgressBar1.Value = 100
WebBrowser1.Refresh()
i += 1
Loop
End Sub
#6
TEMPTii wrote:
You said you wanted it to have an ability to loop for a certain amount of times. To do so, it would be:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 0
ProgressBar1.Maximum = TextBox1.Text
Do Until ProgressBar1.Value = 100
WebBrowser1.Refresh()
i += 1
Loop
End Sub


hmm well theres still a problem, when i debug it and i put in numbers for seconds to reload and times to refresh if just freezes up and stops responding and has to close. :0
#7
Okay... instead of a textbox for the seconds, use a numeric updown and replace textbox1.text with numericupdown1.value
#8
actually what if i used instead of amount to refresh i could just user a second timer to make it Runtime right so how could the code work there as in the progress bar goes up with the time elapsed like i could set it to 1 minute, and the progress bar would reach 100% according to the amount of seconds/time
like a possible way to make the time and put whatever you want in textbox2 like 1:0:0 which would equal one hour of set runtime, i already figured out how to make it stop after a set amount of time with some of Thomas's. it was actually really easy
#9
actually what if i used instead of amount to refresh i could just user a second timer to make it Runtime right so how could the code work there as in the progress bar goes up with the time elapsed like i could set it to 1 minute, and the progress bar would reach 100% according to the amount of seconds/time
like a possible way to make the time and put whatever you want in textbox2 like 1:0:0 which would equal one hour of set runtime, i already figured out how to make it stop after a set amount of time with some of Thomas's. it was actually really easy


Grammar please.
#10
TEMPTii wrote:
Grammar please.


agghhh well what i mean is...

I would like to make my progress bar increase to the amount of time passed/textbox2 with the interval being seconds to each refresh am i correct?
If not please tell me, and that's about all I would like to know on this progress bar subject.

THERE HAPPY?
#11
        Dim i As Integer = 0
ProgressBar1.Maximum = TextBox2.Text
ProgressBar1.Value = 0
Do Until i = TextBox2.Text
WebBrowser1.Refresh()
i += 1
ProgressBar1.Value += 1
Loop
#12
and was that suppose to be for a time integer? cuz this is what i have for textbox 2 and the timing integer, it goes by minutes....
 seconds = Val(Me.TextBox2.Text) * 60000
Me.T1.Interval = seconds
Me.T1.Enabled = True
ProgressBar1.Enabled = True