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:These are the most common for numerical operations. An example of there use is shown below:
- + Addition
- - Subtraction
- * Multiplication
- / Division
This simple code converts Fahrenheit into Celsius.Code: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
There are also inequality operators that we will need to know, these will be essential as well. Below are the most common inequality operators:An example of these in use is shown below:
- = Equals
- <> Not Equal
- < Less Than
- > Greater Than
- >= Greater Than or Equal To
- <= Less Than or Equal To
Part 2: Do LoopCode: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 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: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:Code: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 ClassIn this code, once i is equal to 11, it will continue the sub beyond the Loop.Code: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
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: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:Code: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 ClassNow, in any form, String1 can be referred to without any precedents. For example:Code:Module Module1 Public String1 As String End ModuleThe application already knows that we're talking about Module1.String1 since its been declared using Public method.Code: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
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.
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.Code: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
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.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.Code: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
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
Results 1 to 12 of 12
Thread: Guide to VB.Net: Advanced
- 01 Sep. 2010 10:52pm #1
Error: Title Not Found
- Age
- 29
- Join Date
- Dec. 2009
- Location
- Hell
- Posts
- 2,248
- Reputation
- 248
- LCash
- 0.00
Guide to VB.Net: Advanced
Last edited by TEMPTii; 01 Sep. 2010 at 10:59pm.
- 03 Sep. 2010 01:48am #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 thisCode: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
Last edited by lilvoice32; 03 Sep. 2010 at 01:56am.
- 03 Sep. 2010 01:59am #3
Error: Title Not Found
- Age
- 29
- Join Date
- Dec. 2009
- Location
- Hell
- Posts
- 2,248
- Reputation
- 248
- LCash
- 0.00
- 06 Sep. 2010 09:36pm #4
- Age
- 29
- Join Date
- Aug. 2010
- Location
- In Your FRIDGE with a laptop :)
- Posts
- 1,360
- Reputation
- 89
- LCash
- 0.00
Code: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
"I try to contribute hopefully im doing my job. " :0,
+rep me if you like the pics i draw for you?
Looks like we got ourselves our own ghetto LG chat room
Anyone want an RS account? Add me Pl0x
MSN: sonic_benny@yahoo.com
- 06 Sep. 2010 10:01pm #5
Error: Title Not Found
- Age
- 29
- Join Date
- Dec. 2009
- Location
- Hell
- Posts
- 2,248
- Reputation
- 248
- LCash
- 0.00
You said you wanted it to have an ability to loop for a certain amount of times. To do so, it would be:
Code: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
- 07 Sep. 2010 05:52am #6
- Age
- 29
- Join Date
- Aug. 2010
- Location
- In Your FRIDGE with a laptop :)
- Posts
- 1,360
- Reputation
- 89
- LCash
- 0.00
"I try to contribute hopefully im doing my job. " :0,
+rep me if you like the pics i draw for you?
Looks like we got ourselves our own ghetto LG chat room
Anyone want an RS account? Add me Pl0x
MSN: sonic_benny@yahoo.com
- 07 Sep. 2010 11:59am #7
Error: Title Not Found
- Age
- 29
- Join Date
- Dec. 2009
- Location
- Hell
- Posts
- 2,248
- Reputation
- 248
- LCash
- 0.00
Okay... instead of a textbox for the seconds, use a numeric updown and replace textbox1.text with numericupdown1.value
- 07 Sep. 2010 01:15pm #8
- Age
- 29
- Join Date
- Aug. 2010
- Location
- In Your FRIDGE with a laptop :)
- Posts
- 1,360
- Reputation
- 89
- LCash
- 0.00
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 easyLast edited by sonic_benny; 07 Sep. 2010 at 01:42pm.
"I try to contribute hopefully im doing my job. " :0,
+rep me if you like the pics i draw for you?
Looks like we got ourselves our own ghetto LG chat room
Anyone want an RS account? Add me Pl0x
MSN: sonic_benny@yahoo.com
- 07 Sep. 2010 08:58pm #9
Error: Title Not Found
- Age
- 29
- Join Date
- Dec. 2009
- Location
- Hell
- Posts
- 2,248
- Reputation
- 248
- LCash
- 0.00
- 08 Sep. 2010 09:04am #10
- Age
- 29
- Join Date
- Aug. 2010
- Location
- In Your FRIDGE with a laptop :)
- Posts
- 1,360
- Reputation
- 89
- LCash
- 0.00
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?
"I try to contribute hopefully im doing my job. " :0,
+rep me if you like the pics i draw for you?
Looks like we got ourselves our own ghetto LG chat room
Anyone want an RS account? Add me Pl0x
MSN: sonic_benny@yahoo.com
- 08 Sep. 2010 12:44pm #11
Error: Title Not Found
- Age
- 29
- Join Date
- Dec. 2009
- Location
- Hell
- Posts
- 2,248
- Reputation
- 248
- LCash
- 0.00
Code: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
- 08 Sep. 2010 01:29pm #12
- Age
- 29
- Join Date
- Aug. 2010
- Location
- In Your FRIDGE with a laptop :)
- Posts
- 1,360
- Reputation
- 89
- LCash
- 0.00
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....
Code:seconds = Val(Me.TextBox2.Text) * 60000 Me.T1.Interval = seconds Me.T1.Enabled = True ProgressBar1.Enabled = True
"I try to contribute hopefully im doing my job. " :0,
+rep me if you like the pics i draw for you?
Looks like we got ourselves our own ghetto LG chat room
Anyone want an RS account? Add me Pl0x
MSN: sonic_benny@yahoo.com