Objectives of the Week


New Controls

TextBox.jpg (846 bytes) Text Box   User input. Multiple Lines if necessary. Control over the font properties
Frame.jpg (799 bytes) Frame   Container. Used to hold related controls, especially Option Buttons
CheckBox.jpg (759 bytes) Check Box   Boolean (True or False, On or Off, Checked or Unchecked)
OptionButton.jpg (742 bytes) Option Button Boolean, but only one Option Button per Container can be True
Image.jpg (882 bytes) Image Holds a Picture. The image can be stretched to the size of the control.
Shape.jpg (803 bytes) Shape  Display a Rectangular or Circular image
Line.jpg (699 bytes) Line   Primarily to enhance the user interface

See the chart of some of the Properties, Methods and Events of this week's controls


Naming Conventions

Remember that you only give a control a name when you will reference it in the Event Handler code. Here are the three character prefixes for today's new controls. The format of the control name, as specified in the control Name property is three character prefix in lower case, followed by a descriptive name with the first character capitalized.

Examples:
    txtName, fraColors, chkBold, optSophomore

Text Box txt
Frame fra
Check Box chk
Option Button opt
Image img
Shape sha
Line lin
Command Button cmd
Form   frm
Label lbl

Much of the discussion will refer to this graphic

Example 1

Alignment of Controls

Visual Basic gives you tools necessary to assist you in building a consistent user interface. Your form will look much nicer if the controls are sized and aligned properly. The Format selection on the Menu Bar provides  access to these features. To make use of these tools, you must select multiple controls. Click on a control, then hold down the Control Key, and click on a second and a third control. The selections under the Format Menu selection describe the operations you can perform on these selected controls.

Align Match up the left or top properties.
Make the Same Size Same Height or Width. Especially for Command Buttons.
Horizontal Spacing Move the selected controls close or farther apart
Vertical Spacing Move the selected controls close or farther apart
Center in Form Vertically or Horizontally
Lock Controls When you are all done, this prevents you from accidentally moving a control

 

Form Layout In addition, the location of the form on the desktop when your application starts can be positioned using the Form Layout window located in the bottom right corner of the VB Development Environment. "Grab" the form in this window and position it on the simulated desktop.

 

Keyboard Interface

In most situations, especially when you have a user that has experience using your application, a good Keyboard Interface will make your program much easier to use. Here are some things you need to consider:

Tab Order

Each control on the form has a TabStop and a TabIndex property.
The TabStop is a Boolean (True or False) Indicator. When the user presses the tab key to jump between fields on the user interface, it will or will not stop on the selected control.
The TabIndex property describes the sequence in which the Tab Key will jump from field to field.

Alt-Key Combinations

Want Fast and Direct access to a field on the form?
Modify the Caption of the control to include an Ampersand (&) character. Notice how the Bold Checkbox appears as Bold (Underscore B). The Caption is coded as &Bold. This allows direct access to this check box by pressing Alt-B
The Label and Text Box controls have a special relationship. When the Label and TextBoxes have TabIndexes in sequence, you can code an Alt Key selection on the Label, and pressing that combination will position to the associated text box.
The key combinations must be unique on the form

Default and Cancel Keys

Another Keyboard Shortcut for Command Buttons
Set the Default property to True and when the user presses the Enter Key, this Command Button Click Event is triggered. The exception to this is when another Command Button has Focus
Set the Cancel property to True and when the user presses the Escape Key, this Command Button Click Event is triggered. The exception to this is when another Command Button has Focus
Only one Command Button can be the Default button and only one can be the Cancel button

 

General User Interface Considerations

Tool Tips Use the ToolTipText property to trigger helpful hints as the user positions the mouse pointer over the control
Font Select the proper Font and Color combination for the control at design time.
This can also be modified at run time.

Very Important

The Frame Control is used as a Container. A set of Option Buttons can only have one control set to true per container. For each set of option buttons, use a frame to separate them. In the example above, the Frame as a Container is required to isolate the Graphics selection and the Shape selection. The Frame is used with the Font Checkboxes only to provide a consistency to the user interface.

 

Some Coding Considerations

Intrinsic Constants VB uses literal indicators to represent certain properties in controls. You have seen two examples of this today:

CheckBox.Value = 0 means False or Not Checked
CheckBox.Value = 1 means True or Checked

Shape.Shape = 0 means Rectangle
Shape.Shape = 3 means Circle
There are more here for the shape control

VB provides a set of constants so we don't have to remember that a zero means rectangle, etc.

To set a Checkbox on or off, code:
CheckBox.Value = vbChecked    or
CheckBox.Value = vbUnchecked

To change a Shape control to a circle code:
Shape.Shape = vbShapeCircle

Intrinsic Constants are documented in the Help for the specific control.

With - End With When referencing multiple properties of a control, it is cumbersome to have to fully qualify each property. The With - End With Block provides a shortcut.

Without a With Block:
txtText.Font.Underline = chkUnderline.Value
txtText.Font.Italic = chkItalic.Value
txtText.Font.Bold = chkBold.Value

Using a With Block:
With txtText.Font
      .Underline = chkUnderline.Value
      .Italic = chkItalic.Value
      .Bold = chkBold.Value
End With

This can save you a bit of typing and at the same time, provide some good structure to your Event Handler as you can easily set all necessary control properties at the same time

Default Properties Many controls have what is known as a Default Property

This means that rather that having to specify the property in full Object.Property notation, that you can specify the Default Property by specifying only the Object Name.

Example:
    txtText.Text = "Hello"
    chkBox.Value = vbChecked

The Default Property for the TextBox is Text and the Default Property for the CheckBox is Value. Coding the example using the Default Properties would look like this:
    txtText = "Hello"
    chkBox = vbChecked

If you are referencing the Default Property of a control, you do not have to specify the property name.

After having said all of this:

Don't EVER Use The Default Properties
ALWAYS specify the reference using the full Object.Property notation

This is a bad habit as future versions of VB will probably not support this feature and it is currently not supported in VBScript (which is used in coding for web pages under Windows Internet Explorer)

Concatenation of strings Joining strings literals together is an example of Concatenation. The String Concatenation operator is the Ampersand (&).

txtFullName.Text = txtLastName.Text & ", " & txtFirstName.Text

This example joins the First and Last Name fields into a "Last, First" format

Code Continuation Often, a line of code can become so long that it scrolls off the screen and it is difficult to read. Visual Basic is not at all concerned with this. VB has no problem reading a line of code no matter what the length.

In situations like this, it comes in handy to split a line in two (or three or four ....) The operator to perform this Continuation is the Underscore (see the Shift - Dash on the keyboard)

txtText.Text = "This is " & _
               "a very " & _
               "long string"

Use the Continuation to make your Event Handler code easier to read.

 

Build the Example

Step by Step through the First Example

Make sure that you:

Make sure that follow the basic programming guidelines.


Two Labs

Lab 1 - From the Text - Page 84, VB Mail Order

Step by Step through the First Lab

Let me know when you are done and I will check you off as Complete

Lab 2 - From the Text - Page 84, VB Auto Center

You are on your own.


Assignment

Reading Assignment

Programming Assignment

The assignment is due before the start of class next week

Review the Assignment Packaging Instructions

Flag Viewer

    Page 81, Problem 2.2
    You may substitute the flags of your choice for the ones specified in the assignment

Here is a list of the Program Grading Criteria