Your First VB Program

Creator:
Avtar
Description:
Okay, this tutorial will guide you through the creation of your first program. This will be a very basic program.

Start and Setup

Open Visual Basic and select new Standard EXE from the menu that appears, or if that does not appear, then select File -> New Project from the menu. Click "Project1 (Project1)" in the Project Explorer. Look in the Properties Window. Click where it says Project1 next to Name. Change it to MyFirstProgram. You can only use alpha-numerical characters and underscores (_) when naming Projects. Now, click the + next to "Forms" to expand your form explorer. If it is already done, you're a step ahead :). Click Form1 (Form1) and do what you just did with project, except name it "frmMyFirstProgram" Simple, eh? Now we need to add a caption to our program. Go to Caption in the Properties Explorer and set it to "My First Visual Basic Program." You can use any ascii character when stating a caption, although sometimes they require a special code (like in HTML, to display © you would write ©)

Making Controls and Customizing Them

Now that we have the basics done for our Form, we will start making controls. The end result of this program will be two text boxes where the user inserts two numbers in each box and clicks a button. The program adds the numbers and the answer displays in a text box after an equals sign. Our program will require the following controls: * Text Boxes (3) o txtNum1 o txtNum2 o txtAnswer * Labels (3) o lblPlus o lblEqual o lblInstruct * Command Buttons (2) o cmdAdd o cmdExit For all the text boxes, set their text property to blank. For txtAnswer, set the appearance to "Flat", border style to "none" and backcolor to "button face". Set the visible property to false. It kind of looks like a label now, doesnt it? For lblPlus, make the caption +, for lblEquals, make the caption =, and for lblInstruct, set the caption to: Insert a number in each of the text fields, and click add to get the answer. For cmdAdd, make the caption Add! and for cmdExit, make the caption Exit. Thats it for the controls, make your form look something like this:

Now we can begin the programming part.

The Coding

To open up the coding window, you can right click in the white area and choose View Code or you can double click any object on the form, or the form itself. To start, double click on Exit. It brings up our click event procedure for the Exit command button. Type this in: End. That is quite possibly the simplest piece of code in VB. Run the program by clicking the play button at the top. Click Exit and you see that the program closes. That was easy. :) Now, double click on Add!. This is where it can get confusing for a extreme beginner, but just follow with me because it is really easy. You should have this shown in front of you right now:

What we want to do is take the text in txtnum1 and add it to the text in txtnum2. This is a very simple code, but will explain a lot in the workings of Visual Basic. The first code you want to add is the core of the program. Normall, we would stop here, but I am going to introduce you into some other aspects of Visual Basic. Make the code for cmdAdd_Click() this:

Run the program by clicking the play button at the top and enter in two numbers in the two text fields and click Add!. It gave you the correct answer. Congrats, we have the brain of our first program. But What If the User Does'nt Put in a Number If the user does not put in a number, it will cause an error. This is easy to detect and fix. Stop the program and erase the code you just added for cmdAdd_Click().

We are going to be using an if statement. Ifs basically check to see if a condition is true or not true and then display something based on the results of that check. For this instance, we are going to check to see if the text entered in the two text boxes are numbers, and if they arent, we will display an error message. The first thing we do is open up our if by typing If. Now we need to tell the program what we are checking. In this case, we are checking to see if the text in txtNum1 and txtNum2 is numerical or not. Immediatly after our If, type IsNumeric(txtNum1.text) and IsNumeric(txtNum2.text). To tell the program we are done stating what we want it to check, you type Then and hit enter. Now, in this area is what we put if the condition we stated above is true. For this program, we put what we had before. Your code block should look like this:

Now we move onto telling what the program what to do if the condition was not true. We accomplish this by writing Else on a new line. Hit enter again to start a new line and this is the area we add what we want the program to do if the condition was false. Same as what we did before in theory, but not in code. For this program, we are going to add a message box. This is accomplished by using MsgBox (this is a preinstalled function with Visual Basic). Message Boxes are quite easy to use, and can get quite elaborate in design, but for the purposes of this program, I am only going to make a simple one. Once the user hits "OK" on the message box, we want to send them back to the text field with the error. This is accomplished by writing txtNum1.setfocus. Once this is done, we can end our if statement by writing End If. Mimick my code:

You can make the message box say whatever you want.

Conclusion

Well that is basically it, your first Visual Basic Program. A very easy tutorial to follow. Please post if you have any trouble. Good luck with your VB Future.

Challenge

Create a program that either subtracts or multiplies two numbers together. Make sure you check to see if the user enters in a number. Upload and post your programs in this thread. Good luck.

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <b> <u> <i> <hr> <img src <url=
  • Lines and paragraphs break automatically.

More information about formatting options