Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  A Practical Guide to VB.NET for Beginners
A Practical Guide to VB.NET for Beginners
by Michael Stahle | Published  01/20/2003 | .NET Newbie | Rating:
Michael Stahle

I am currently a computer and financial consultant for Brandaris who consults for United States Air Force. We provide a wide range of On-Line tools, intranet support and development, along with a gambit of financial analysis. I am a senior at Utah University, in the Business Information Systems program. If you need to contact me, or are in need of any service that Brandaris provides, please email me at m.stahle@attbi.com or michael.stahle@hill.af.mil.

I have three years of database design and administration, along with 4 years of Visual Basic Programming.

 

View all articles by Michael Stahle...
A Practical Guide to VB.NET for Beginners

Introduction

The new .NET framework that Microsoft has recently released is quite different than the framework that VB6 is based on. When I first started working with VB.NET, the true Object Oriented Programming (OOP) threw me for a loop. This guide will explain to you what OOP is, and how to build your first program.

OOP has been around for a while, though never truly in VB until VB.NET. But what is OOP, you ask? OOP is exactly what it says: Object Oriented Programming. Each thing (class, button, text box, etc) that you use while programming is an object. Back in the QBasic days, all programs ran top down, or what you coded first was what happened first (I am grossly simplifying). For example, when you coded the program to display a line of text, or to draw a circle, it would draw a circle or display text in the order that you coded it. In OOP, your code is attached to an object, and when an event occurs to that object, then the appropriate code is executed. For example, you create a button and attach code to the button to "fire" when the button is clicked; the code will not "fire" or run until the button has been clicked. Now the code that is ran when the button is clicked is similar to the code back in the day before OOP, but is not quite the same. The difference lies in the fact that the code in OOP is based on classes. Now I know what you are asking, what is this class thing? A class is exactly that, a thing. It could be anything, and in the case of VB.NET it is everything. Let's say that there is a class called Book. Every property that is associated with a book is encapsulated in its class. For example, the total pages in the book is a property of the book, the color of the book is another property, and if the book is hardback or not is another. Let's take an example from VB.NET. A button in VB.NET is a class and an object. This object has many properties. One such property is that of the caption on the button, another is the color of the button, and another is if the button is visible/accessible or not. Each of these properties of the class can be accessed and changed (generally speaking). Now that we have some of the very basic principals down, we can start by creating our very first program.

Our First Project

In order for us to create our program, we must first open a new project we can do this by clicking on the File menu option, then selecting new, then project. This will bring up a form asking you what kind of project you would like to create. For this project, we would like to make a Windows Application in VB.Net. Select the folder called Visual Basic Projects, and select Windows application. For this project, we will be calling our program "Hello World." Select where you would like to place the program that you will create, and when you have done all that, you screen should look similar to the following graphic:

(Figure 1.1)

Now it is time for a basic tour around the screen. You will see a pretty familiar menu system on the top of the screen. Don't be fooled (maybe "note" would be more appropriate???), there are some very powerful wizards enclosed in these menus, wizards that can save you hours of hard coding. You will also see a toolbar on either side of the screen (depending on setup and layout). You should also see a property bar to either side of the screen.

(Figure 1.2)

Take a moment to look around. After you feel a little more familiar with the screen, we can start creating our new program.

From the toolbar, select windows forms, if it hasn't already been selected by default. From this menu you can select many different objects that you can add to your program. Scroll down the bar until you come to the RichTextBox object. Click it. Now if you move your mouse over to the form (it should have the name of Form1 on it) you will notice that the mouse cursor has changed. Now like almost any windows application, just drag and draw the object. If you make the object too small, that's okay, because you can resize the image easily by dragging one of the white squares on the outside of the image. After you have drawn (or added an object) you will see the object created in your form. Now it is time to use the properties bar. Select the TEXT option in the properties bar and change the text from RichTextBox1 to Hello World. Click the form and you will see that the text inside the RichTextBox object has changed to Hello World. Now we can also do this with the form. Click the form, or select Form1 from the dropdown box on the property bar, and change the text property to Hello World.

Now you should have a project that looks similar to figure 1.3.

(Figure 1.3)

Well we have a great looking project but it doesn't do anything. Lets add a few buttons to add some functionality to our project. Once again, go to the Toolbox bar. This time select Button from the options. Draw the button onto the form, and then repeat the process until you have drawn three buttons on to your form. Once you have completed drawing the buttons, change the texts on all three buttons. Change button1's text to Clear. Change button2's text to Hello World. And change button3's text to Color. Now that you have completed that, we can start coding. Double click the Clear button. When you double click any object in a project, it will bring up the coding screen. Enter the following code into the program:

RichTextBox1.Text = ""

Your coding screen should look as mine does in figure 1.4.

(Figure 1.4)

As I have indicated earlier, everything in VB.NET is an object of some sort, and RichTextBox1 is no different. To access any property associated with RichTextBox1, all you must do is call it by name, add a "." (period) and then call its property by name, as is illustrated in the example above.

Lets examine the code a little closer. As we have just discussed, the object RichTextBox1 can be referenced by calling it by name, along with any one of its properties. When we have changed the RichTextBox1 text property before to Hello World, we set its initial property value. The code attached to the event of clicking button (Button1_Click) changes the initial RichTextBox1 text property from Hello World to nothing (hence the Clear text value for button1). To test your code, click the play button (the triangle on the menu bar). After a few moments, your project will appear. Click the Clear button and notice that the text in the RichTextBox1 object disappears. Close your program, and return to the design screen. We will now add code to the other two buttons. Double click the Hello World Button, and access the coding screen. Enter the following code into the program:

RichTextBox1.Text = "Hello World" 

This will add the words Hello World back into the RichTextBox1 object. Once again, we are accessing the RichTextBox1 object by calling it by name. We are also access the RichTextBox1 text property in the same way as we did with the Clear button code.

Click back to the Design screen and double click on the Color button. The code for this button is similar to the other button's code, but we will be accessing another class other than the RichTextBox1. Lets examine the code that will go into this button:

RichTextBox1.SelectionColor = Color.Red

As you can see, we have called RichTextBox1 again, but this time we have accessed its SelectionColor property. This property changes (or returns) the color of selected text within the parent object (in this case RichTextBox1). The difference in this code from the others is that we are setting the property to a value from another class, other than a value (or empty value) that we code. Color in this case is a class. On of its properties is red. So you can see that we are going to set the Selected color in the RichTextBox1 to red. Say we wanted to change the color to blue instead of red, in that case we would set the RichTextBox1.SelectionColor = Color.Blue. The final coding screen should look similar to that in figure (1.5)

(Figure 1.5)

Test your code in the same manor as before, but this time select some text from the RichTextBox1 object and click color. If you don't have any text due to clicking clear, click the Hello World button, or just type some of your own into the rich text box.

Now that you have the general idea about objects and how to access their properties, I suggest for you to practice on some of the other objects that are found in the Toolbar.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.97093023255814 out of 5
 172 people have rated this page
Article Score39540
Sponsored Links