Wyo VB Lecture Notes - Objects, Methods, & Properties
Objective #1: Know how to place textboxes, labels, and buttons on a Visual Basic form.
- Textboxes, labels, and buttons are three very useful kinds of objects to use in a VB program.
- There are several ways to place objects on a form.
- You can double-click a Toolbox icon in order to place that object on the form. You may have to drag objects around the form in order to be able to see newly created ones.
- You can drag a Toolbox icon onto a form to place an object there.
- You can single-click the Toolbox icon to select it and then click and drag on the form itself to place and size an object.
Objective #2: Be familiar with the naming convention used for forms, textboxes, labels, and buttons.
- You must also use the conventional VB prefixes when naming objects. Always begin an object's name with the correct prefix (usually 3 lowercase letters) that identifies the type of
object that you are naming. For example, the name of a textbox must begin with txt. The name of a label must begin with the prefix lbl. A button must begin with the prefix btn. This method of naming variables is called Hungarian Notation.
- After the prefix, you must use a descriptive name for the object. If the descriptive name consists of two or more words,
each word should begin with a capital letter. There should be no spaces in the name of an object and I even recommend against using the underscore character ( _ ). Good names for a button would
be btnClick or btnComputePrice. The names btnclick, btncomputeprice, btnComputeprice,
or btn_compute_price should not be used.
- VB automatically assigns the default name of Form1 to the first form in your project. I do not recommend that you rename this form even though it
does not begin with the frm prefix for forms.
- You should always rename textboxes, buttons and most labels AS SOON AS you place them on a form.
- For some labels that you include on a form and occasionally other objects, you may use the default names (e.g. Label1, Label2)
that are assigned by Visual Basic. If you do not refer to an object within your code, then you can use its default name.
- A partial list of object prefixes can be found at support.microsoft.com/default.aspx?scid=KB;en-us;q110264 even though it doesn't contain some objects such as buttons that were added to recent versions of VB. Another list is found at http://www.rwc.uc.edu/thomas/Intro_OOP_Text/Misc/VB_prefixes.html
Objective #3: Use Click methods to make a program interact with the user when he/she clicks the mouse.
- Visual Basic makes it easy to create event-driven programs. An event-driven program is one which responds to users' actions such as mouse clicks and mouse
movement. Before the 1990's, programming languages such as older versions of Pascal, C, FORTRAN, and COBOL were considered to be procedural. The programmer simply wrote lines of code that executed
with a sequential flow of control. The compiler or interpreter determined the order of execution of different tasks based on the programmer's original design. Such programs were not as interactive
as modern Visual Basic event-driven ones. However, sometimes user interactivity is a hindrance to effective program execution.
- A useful object that is found in practically every VB project is a button. A button allows the user to interact with the VB project since it can be clicked by the
user.
- The proper prefix to use when naming a form is btn.
- The Text property of a button is used to present the word or short phrase that the user will be able to read on
the button.
- A button's Click method contains code that executes when the user clicks the
button on the interface of the form. For example, the following method named btnExit_Click causes the program to end when the user clicks
this button:
Private Sub btnExit_Click(ByVal sender As System Object, _
ByVal e as System.EventArgs)Handles btnExit.Click
Application.Exit()
End Sub
For now, until we learn about menus, you should include an Exit button in the lower-right corner of every form.
- To have VB create an empty button Click method, you should double-click on a button. Then you can type whatever statements you'd like the computer
to execute when the user clicks the button.
- A form has a Click method as well that executes when the user clicks anywhere on the form. The following method
named Form1_Click causes a message box to appear but only when the form is clicked
Private Sub Form1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Click
MessageBox.Show("Hi")
End Sub
- To type the MessageBox statement into the form's Click method, it is easiest to open the code window and change
the ClassName listbox at the top-left of the code window to "Form1 events" and then change the MethodName listbox at the top-right of the code window to "Click". VB will create
an empty Form1_Click method that you can fill in with statements.
- Textboxes and labels also have Click methods that can be created similarly.
Objective #4: Identify objects, properties, and methods and explain how they are different from syntax and keywords.
- A form is sort of the foundation of a program. It is an example of an object. But it also acts as a container on which you can place other objects.
- Objects (also known as controls) are the things that make up a program. Examples of objects include forms, textboxes, buttons, labels and
MessageBoxes.
In the statement,
lblHello.Text = "Hello World"
lblHello is the name of a label object.
- Each kind of object has its own set of properties. An object's properties affect the way it is displayed and the way that it interacts with the user and other
objects. Examples of properties include Name, Text, Font, BackColor, & ForeColor.
Referring to properties is tricky. You must first include the name of the object, followed by a dot operator ( . ), and then the name of the property.
In the statement
lblHello.Text = "Hello World"
Text is the name of a property. It is a property of the label named lblHello.
In the statement above, the value of the property is being set to the phrase "Hello World" in this statement. Setting the label's Text property to "Hello World" is called assignment.
Therefore the equals symbol is called the assignment operator because the phrase "Hello World" is being "assigned to" the label lblHello . The phrase "Hello
World" is
called a string.
Do not confuse the Text property with the Name property of an object. The Text property usually
appears on the screen. The Name property is used to refer to the object in the source code.
- Methods are actions that a programmer can use with objects. Examples of methods include Load, Click, & Clear.
In the statement
txtUserInput.Clear()
Clear is the name of a method. It is a method that works with textbox objects. This statement causes the Text property
of a textbox named txtUserInput to be emptied. But the following statement also clears a textbox named txtUserInput
txtUserInput.Text = ""
In the statement above, using two double quotes next to each other puts nothingness into the textbox. Two double quotes next to each other is called the empty string (which
is also called the null
string).
In the statement
Application.Exit()
Exit is the name of a method. This statement can be used to end the program. Notice that methods are always followed
by a set of parentheses.
The statement
Label1.Text = "My Name is Earl"
causes the phrase "My Name is Earl" to appear in a label named Label1.
The statement
Label1.Text = "My Name is " + Textbox1.Text
causes the phrase "My Name is " to be concatenated to whatever happens to be typed into Textbox1 at that moment. Concatenate means to join together to strings (i.e. phrases). Visual
Basic causes the plus symbol in this case to act as a concatenation operator and not an addition operator.
The & symbol also works as the concatenation operator so the statement above could be rewritten as
Label1.Text = "My Name is " & Textbox1.Text
However, if a number is typed into Textbox1 and the + symbol is used as a concatenation, then the statement
Label1.Text = 10 + Textbox1.Text
will cause the sum of 10 plus that typed number to display in Label1. In this case, VB caused the plus symbol to act as an addition operator since it knows that both arguments are number
values.
You can also display strings or numbers in a message box. The statement
MessageBox.Show("My name is Earl")
shows the string "My name is Earl" in the middle of a MessageBox. The statement
MessageBox.Show("My name is " + Textbox1.Text)
shows the string "My name is " followed by (i.e. concatenated with) whatever happens to be typed into Textbox1.
- Classes are patterns that are used behind the scenes to create an object. Technically, when you double-click an icon in the Toolbox and place an object on the form, the object is
actually being created from a class definition. In the Hello World program above, Form1 is the name of a class.
- Syntax is the set of symbols and operators that are used in Visual Basic including the period symbol, the equals symbol, double
quotes, parentheses, etc. along with the rules that you must follow in order to use them correctly. Syntax is to the Visual Basic programming language like grammar is to the English language.
- Keywords are commands that are defined in Visual Basic. Examples of keywords are Public, Class, Private, Sub,
and End. These words do not represent objects, properties, or methods. Rather they are defined keywords. An error will occur of course if you mispell or overlook a
necessary keyword.