Wyo VB Lecture Notes - Objects, Methods, & Properties
Objective #1: Use forms appropriately.
- A form is a basic building block of a Visual Basic project. Eventually, you'll be creating projects that consist of many forms. You'll also learn how forms can be
reused to save programming time and to give more consistency to a large project.
- A form has number of useful properties including the Text property. It is best to set the Text property
of a form to an appropriate phrase. The form's Text property appears in the blue title bar at the top of the form.
- The first default form of a VB project is often saved with the file name of Form1.vb. The class that goes along with this form is also named Form1.
For now in this VB course, it is not recommended that you change the this file name or that you rename the class even though it does not begin with the conventional form prefix frm.
- You can think of a form as having two basic parts, its interface and its code. The interface is what the user will see when he/she executes the program. The code
is the behind-the-scenes part of the form that contains the crucial logic and code that makes the form work that way the programmer intends it to work.
- A form and the objects on a form are measured in pixels. The size of the pixels on your monitor depend on your computer's screen resolution settings. Popular screen
resolution settings are 640 pixels by 480 pixels, 800 pixels by 600 pixels, or 1024 pixels by 724 pixels where the first number is the width of the screen and the second measurement is the height of
the screen. Larger numbers of pixels in the resolution means the screen looks sharper with more detail. For now in this course, it is not recommended that you resize a form to make it fill up more
of the computer screen.
- The Size property of a form is used to determine its size. Actually, it is the two subproperties, Height and Width,
of the Size property that determine the size of the form. The Location property of a form is used with its two subproperties, X and Y,
to determine the location of the upper-left corner of the form on the screen.
- The Load method of a form appears in the code window when you double-click anywhere on the interface of the form.
Any code typed into the form's Load method executes as soon as the form is loaded. The default form of your project loads as soon as you (or anyone else) execute your
project.
Objective #2: Know how to place textboxes, labels, and buttons on a 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 #3: 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 #4: 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()
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 the method's body statements that you would 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()
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 #5: Identify and use objects and properties.
- In English class, you learn about grammar and the rules for writing sentences and paragraphs. In computer programming, we refer to this as syntax. 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.
- 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.
- 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.
- When you place an object on a form, you should immediately give it a proper name. That is, you should type something into its Name property. The Name property of an object should begin with a proper prefix. Label's should start with lbl, button's should start with btn, and textboxes should begin with txt. The next word in the Name properaty should begin with an uppercase letter and the word should be meaningful. For example, if the purpose of a button is to clear some labels than a good name would be btnClear. If the purpose of a label is to display the directions to a game then a good name would be lblDirections or even lblGameDirections. Notice the use of an uppercase G and uppercase D. Name properties cannot have spaces in them so using the uppercase letters makes it easier to quickly read the name of an 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. 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 Name property is used to refer to the object in the source code. Name properties can never have a space. Also, Name properties should begin with an appropriate prefix (e.g. lbl, btn, or txt).
- The statement
lblOutput.Text = "My Name is Earl"
causes the phrase "My Name is Earl" to be stored in the Text property of a label named lblOutput.
The statement
lblOutput.Text = "My Name is " + lblName.Text
causes the phrase "My Name is " to be concatenated to whatever happens to be typed into txtName at that moment. Concatenate means to join together to strings (i.e. phrases). Visual
Basic causes the + symbol in this case to act as a concatenation operator and not as an addition operator.
However, if a number is typed into lblSomething and the + symbol is used as a concatenation, then the statement
lblAnswer.Text = 10 + lblSomething.Text
will cause the mathematical sum of 10 plus that typed number to display in lblAnswer. In this case, VB caused the plus symbol to act as an addition operator since it knows that both arguments are number
values.
- The following statement clears a label named lblPhrase
lblPhrase.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 (aka the null
string).
Objective #6: Be able to write out a whole method
- Methods are actions that a programmer can use with objects. Examples of methods include Load, Show, Click, Exit, & Clear.
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.
- 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.
- You should be able to write out a whole method. A method always begins with a line that starts with Private Sub.... and a method ends with the line of code End Sub. One or more statements are typed inside the body of a method. The body statements are indented for good style.
For example, if you are asked to write a method that displays "Hello Jupiter" in a message box when a button named btnClickHere is clicked, here is the answer you should give
Private Sub btnClickHere_Click()
MessageBox.Show("Hello Jupiter")
End Sub
In this class, you are not expected to write the complicated code that goes in the parentheses on the first line of the method. The name of the method is btnClickHere_Click however the name of the method includes the name of the object that is being clicked (btnClickHere) followed by an underscore ( _ ) which is followed by the event (i.e. user's interaction with the computer.)
- If you are asked to write a method that causes the program to end when a button named btnExit is clicked, your answer would be
Private Sub btnExit_Click()
Application.Exit()
End Sub
- If you are asked to write a method that causes the phrase "I attend Wyomissing Area High School" to appear in a label named lblOutput when a button named btnSchool is clicked, your answer would be
Private Sub btnSchool_Click()
lblOutput.Text = "I attend Wyomissing Area High School"
End Sub
- If you are asked to write a method that causes the phrase "Hello World" to show up in a message box as soon as the program is executed, your answer would be
Private Sub Form1_Load()
MessageBox.Show("Hello World")
End Sub
Notice that the event is Load since it automatically executes first when a program is started.
- If you are asked to write a method that displays the total cost of purchasing five items at $50 each in a label named lblTotalCost when the
user clicks a button named btnCompute, your answer would be
Private Sub btnCompute_Click()
lblTotalCost.Text = 5 * 50
End Sub
- If you are asked to write a method that displays the total cost of purchasing the number of items typed into a textbox named txtInput where items at priced at $50 each and to show that total cost in a label named lblTotalCost when the
user clicks a button named btnCompute, your answer would be
Private Sub btnCompute_Click()
lblTotalCost.Text = txtInput.Text * 50
End Sub
This assumes that the user types a number such as 5 into the textbox.