Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
This part will continue from the last one so we will continue on the project we had in Part number 1, in this Part we will add a few objects to the form and also add some simple code to it. So after you have loaded the project we can start.
To the left you can find something called Toolbox. Here you can find all default objects(it's also possible to add more of them). To add one to your form just click and drag from the list to the form. You can modify this object a bit by just using the mouse but you have a lot more things you can modify in the properties menu to the right.
Now add one button, one label and one textbox and change these things in the properties window:
The button:
Code:Name="cmdOK" Text="OK"
The label:
Code:Name="labName" Text="Please enter your name and press OK"
The textbox:
Code:Name="txtName"
The thing we just did was to rename them as we wanted and also to change the text of the button and label, after placing them where you want the form could now look like this if you did everything correct:
Now we're going to add an event. Events are a sort of sub(a block of code, but we comes to that later on in the series) which will be called(the code in the block will run once) when something is happened to the object. There's 3 ways to add an event.
The first way is to add the default event, it's just by double-clicking on the object in the design view. The default event for a button is when it's clicked and for a textbox it's when the text is changed etc. When doing this you will automatically be taken to the code view. Another way to open the code view is to right click on the form in either the design view or the solution explorer and then select view code.
If we are in the code view we can add which event we want by just selecting the object and the event we want at the top of the code view. Then the event block is added automatically.
The third way is to write them on your own, this is not recommended for new programmers.
Now we want to add a click event block for the button. Since the click event is the default event for buttons you can just double-click on the button.
After doing this we will have this code:
Code:Public Class frmMain Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click End Sub End Class
This thing about a class:
Is basically all code that exists in the form. What it's actually are and how to create and use your own classes we'll talk about later on. The only thing you actually need to know now is that we should put all our code in it.Code:Public Class frmMain End Class
What we're actually interested in is the newly created event block:
All code we put in this will be run each time the user presses the button.Code:Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click End Sub
Add this code inside the block:
Code:'A messagebox will be showed with the text from the textbox MessageBox.Show(txtName.Text)
The first line is just a comment, comments are created by adding a single quote at the beginning of it. You should use comments a lot, it's good if someone else will look at your code or if you look back on your code months later and have forgot what everything does.
In the second line you can see:
in this way we access the text property of the texbox txtName. Then we are using the text as the text of a messagebox. So the second line will show a messagebox with the same text as the textbox.Code:txtName.Text
Save your project now.
Then it's time to test the program, press F5 to start the debugging. Test to write different things and then press Ok. If I enter "Vswe" in the textbox and then press Ok it will look like this:
You have now created your first program, we'll continue in part 3.
Last edited by Vswe; 11-02-2009 at 01:51 AM.
Nice and gentle intro into code, good work. +rep
Nicely done. +rep
I had a chance to see a coder working on VB2008 last week. It's not a bad language.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks