Guide to VB.Net: Beginner
This guide will show you how to begin VB.Net with no prior knowledge whatsoever. we will cover the basics to understanding how to use VB.Net to do simple things like string combination or simple arithmetic to understanding VB.Net's GUI designer as well as its relation with code.
Part 1: Base Code
When you first create a new project and open the coding window, the only thing that should be present is:What this code does, is defines the class named Form1 and contains it within the two tags. All code associated with the class Form1 must be within the class tags. This also applies to all other containers such as the one below:Code:Public Class Form1 End ClassNow after seeing this code you may think, "I can't do that, look how complicated that is!". You're wrong, I simply double clicked something and that code was generated. Anyhow, this is a Sub. This Sub is triggered when MyBase.Load (the base form loads) and all code within the Sub tags is executed. The Class and Sub containers are the only thing you will be needing trry about if you're a beginner, the other containers come later.Code:Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub
Part 2: Declarations
To think logically, can you do something to what isn't there? If you're not an attention-whore-****** you would have said no. This is also found in VB.Net not only in the designer, but in the code. Not all of the coding revolves around what is visible, so there are code commands to do what the GUI can as well as what it can't do.
Let's say that I want to store a string in the code and not in the GUI. We would need to declare a string to give a value to. In order to do this we will need to use what we know about containers to understand how these objects can be accessed. If a Class contains Subs, then if we declared something for the Class, wouldn't it be accessible to all Subs within the class? Yes. If we declare it into the Sub, wouldn't it only be accessible to that Sub? Yes. What this means is that depending on where we declare the object (string), it will be able to be accessed by different things.
Now you're probably wondering how do you declare? Well there are 2 ways that we'll need to know. Dimming, and Public. Everything in the paragraph above applies if we use Dim to declare objects, however if we use Public to declare our objects, that object is not only accessible by everything in the Class it's contained in, but every other Class in the project too. Now, there are different parts to declaring which are the name of the object, the object type, and whether it is cleared or not. Observe the examples below:What just happened? Well right after the first Class tag, we Dimmed String1 as a string and Integer1 as an integer. This means that String1 is now a text element in the project, and Integer1 is a numerical value. They are null values because they haven't been defined, so they are defined within the Sub Button1_Click. For Integer1 = 0 To 3 (you will learn this later) defines the integer first as 0 and loops at Next Integer1 and for each loop Integer1 changes from 0 to 3. Within the For/Next statement, it says String1 = "Number & Integer1. What this does is defines String1 as Number <#>. For example, if Integer1 was 3, String1 would equal Number 3. These things will be further explained in the intermediate tutorial.Code:Public Class Form1 Dim String1 As String Dim Integer1 As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Integer1 = 0 To 3 String1 = "Number " & Integer1 Next Integer1 End Sub End Class
Part 3: The Design Interface
When you go to the designing pane, it should look something like this except without all of the contents in the form. To the left you can see a large toolbox filled with various controls. If you click these controls and click anywhere on the form, it will be placed there. To the right you can see the properties box which contains all aspects of the selected control that you can edit.
Now lets slow down, how about we place a button onto the form. What we do is click on "Button" in the tool box, and click or drag on our form and it will create the button. Now, double click that button and you will be redirected to the code box. What happened? This should be your current code:Now, what you just did is created a Sub that handles the Button1.Click event. Different controls will create code with different events by default so we'll look at the code pane. If you select the space/text between the Sub, you will see this at the top of the pane. There is a dropdown list that says Click, now press it and see what various events can be triggered by that specific control. Select one and the code for it will be generated. It's very basic.Code:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End Sub End Class
As you saw when you created the button, the text on the button was Button1. You can change this text by selecting the Button1 control and looking at the properties pane. Scroll until you find text and in the textbox it will say Button1. You can edit this if you aren't debugging. You can also change other properties and see what changes occur. There will be an explanation at the bottom of the properties box of the selected property anyways.
Part 4: Programmatic Editing
Sometimes you may want a visible control to change if an event is triggered. For instance, maybe I want a Button to change its text when it's clicked. This would be done with the code below:Now, you saw that we used the Button1.Click event so when Button1 is clicked, Button1.Text = "Clicked". When you type in the object name (Button1) and put a "." after it, there will be a list that appears with all properties or functions that the object can do. I chose text, which edits the displays text of the object. Since "Clicked" is in quotes, that defines it as a string which means that when Button1 is clicked, it's text changes from Button1 to Clicked. The text isn't the only thing that can change, all the aspects (except for read-only properties) can be changed programmatically.Code:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Button1.Text = "Clicked" End Sub End Class
Part 5: Lesson Test
Now it's your turn, create a program with a button, a label, and a textbox. Have the program change the text of the label to the text of the textbox when the button is pressed. If you can do this, you're ready for the next tutorial.
Results 1 to 6 of 6
Thread: Guide to VB.Net: Beginner
- 24 Aug. 2010 10:51pm #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: Beginner
Last edited by TEMPTii; 01 Sep. 2010 at 10:56pm.
- 25 Aug. 2010 01:01am #2
- 01 Sep. 2010 09:31pm #3
Awesome! thanks. moving on to intermediate. is this correct?
Code:Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Button1.Text = "Success!" Label1.Text = TextBox1.Text Me.Text = "Success!" End Sub End Class
- 01 Sep. 2010 09:55pm #4
Error: Title Not Found
- Age
- 29
- Join Date
- Dec. 2009
- Location
- Hell
- Posts
- 2,248
- Reputation
- 248
- LCash
- 0.00
- 02 Sep. 2010 10:16pm #5
k sweet. thanks
- 19 Nov. 2010 07:32am #6
This looks amazing, I'll get started on it tomorrow after I finish my English essay haha.
+rep!|[BCK]| Private Rank 1