Visual Basic Programming

Page 2

Now it is time to add some objects.

With your program and form still open, double click on the label control in the toolbox. The objects are alphabetized.

  1. Choose the (Name) property
  2. Type in a name, with the identifier lbl. Examples: lblShow, lblPrice, lblTotal etc.
  3. Choose the objects Text property
  4. Type anything you want, then press enter; notice the label's caption changes.
  5. Choose the AutoSize property and set it to True.

Now add a command button from the toolbox by double-clicking or selecting and dragging. With the command button selected,

  1. Choose the (Name) property in the properties window
  2. Type in a name, with the identifier cmd. Examples: cmdChange, cmdCalc, cmdClear etc.
  3. Choose the objects Caption property, and type in &Change, or anything you want. The ampersand (&) makes the character to the right of it an access key; in this case, the "C" is the access key. The user can hold Alt then C to use the command button.
  4. Double-click the command button on the toolbox again to add another command button.
  5. Name it cmdExit. Choose the caption property and type "E&xit" which makes the access key "x."
  6. Make sure the command button is not covering the label.
  7. Run the program. Notice you can click the buttons, but nothing happens. Of course not! There is no code in the objects.

Try changing properties of the form. You may want to do this on a different form. If you want, you can add a form by choosing Project > Add Form, and selecting the form icon.

**Notice how when you position the object on the form that guide lines are used. These help the developer make a neat and presentable interface.

Now lets write some code! To access the code window, double click on the cmdChange object. You can also click the white icon in the Project Explorer window to access the code window.

Your window looks like this:

Public Class frmProgram

Private Sub cmdChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButton.Click

End Sub

End Class

To explain this code, I will begin with the cmdExit sub-procedure. A Sub-Procedure is simply an execution that happens on an event. That is how this code executes. The code controls what the computer will do. The Private Sub cmdExit_Click() line simply defines a sub-procedure. You noticed that Visual Basic creates this in the code window when you click an object. The End word you entered in code is a preset keyword for the programming language, in this case, ending the application. Keywords are words which have a special meaning in a programming language. In Visual Basic, some of these words include Dim, For, While, Public, Private, etc. The code we wrote, lblMessage.Text  = "I just wrote code", lblMessage is the object and Text is the object's property. A property of an object is like a part of an object. This label object displays text, and thus the property text determines what the label displays.The "=" sets the property equal to something, resulting in the message in lblMessage to change. The property text is always equal to some sort of text, so in order for the computer to take it literally, the caption needs to be equal to something in quotes. Another way to change the property is to set it equal to a string, which is a programming term for text. Strings are discussed later in this tutorial.

Just like the label object, the text property determines the text that initially appears in the text box.

The code reads: 

Private Sub cmdChange_Click()
    lblMessage.Text = txtMessage.Text
End Sub

Just as its says in code, the label object lblMessage's text will equal the text in object txtMessage when the command button cmdChange is clicked.

Now lets learn to use another object before the next section. Specifically, the frame object. The frame object groups objects in a form.

You may have to shrink the frame object you entered in order to right-click the form and not the frame. As long as the squares known as handles are around the form after right-clicking, you will see the option.

Time to move on. I would encourage you to experiment with the basic properties for some of the simple objects. Remember: Object.Property
Don't forget to name your objects and make sure the event is set properly.

Onto Page 3

Back to Top

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30