Introducing More ControlsText box Check boxOption button
Command buttonframe image
Text Boxes
Used primarily for user input• It holds the value input by the user as characters
Three letter prefix is txt Use the Text property of text boxes in program code to
assign an input value• Example: txtMessage.Text=“Enter your name here”
Alignment property changes the alignment of text but you must also set the Multiline property to True for it to be effective
• Set at design time only• 0-Left Justify;1-Right Justify;2-Center
Frames
used as containers for other controls and to group like items on a form.
Three letter prefix is fra Set the Frame Caption property to the words that you wish
to appear at the top edge of the frame
Check Boxes
allows the user to make choices any number of the boxes may be selected or deselected Three letter prefix is chk Value property is set to 0 if unchecked, 1 if checked and 2
if grey or disabled• Use the Value property in your event procedure code to
determine the user’s selections Caption property is used to specify text next to the box
Option buttons
only one can be selected Generally place several options in a frame to form a group
Create frame first, select object and use the crosshair to draw the option
Doubleclicking the control on the toolbox automatically places it on form
Once on the form, it becomes part of the form group even if you later drag it into a frame
Value Property is True if option is selected and False if not Caption property is used to specify text next to the box Three letter prefix is opt
Images
A control which will contain a graphic The control is first placed on the form and then the Picture
property provides a Load option to include the desired graphics file
• File extension of .bmp, .gif, .jpg among others Stretch property = True causes the picture to fill the
control Visible property = False causes the picture to be invisible
Setting a Border and style
The Appearance property of many controls can be set to 0 - Flat or 1 - 3D
To make a label or image 3 dimensional first provide it with a border
The Shape and Line Control the shape tool in the VB toolbox (not drawing toolbox)
provides the ability to put rectangles, squares, ovals and circles on a form Type of shape is assigned through the Shape property 0-Rectangle, 1-Square, 2-Oval, 3-Circle, 4-Rounded Rectangle, 5 -
Rounded Square 3 letter prefix is shp
Use the line tool on the VB toolbox to provide for lines on a form Change the properties for thickness and color using Borderwidth and
Color 3 letter prefix is lin
Working with Multiple Controls
You can select multiple controls and treat them as a group, including setting common properties at once, moving them, or aligning them.
The properties windows display properties common to the group after the group has been created
Combining controls into a group
To select Drag a selection box around the controls to group or/ Ctrl or Shift select each one
To deselect Click anywhere on the form not on the control
To align and resize a group of controls
Use the format option on the menu and select the Align or Make Same Size submenus
Measured in Twips – 1/1,440 of an inch • a control that that has a width of 1,440 twips is an inch wide
Standards for Designing the User Interface
Make your programs easier to use by following Windows standard guidelines for colors, control size and placement, access keys, default and cancel buttons, and tab order
Windows are primarily grey White text boxes Grey background for labels
If a message will be displayed or a calculation results – border the label
If only a caption on the screen – no border
Defining Keyboard Access Keys
Define keyboard access keys by including an ampersand in the caption of command buttons, option buttons, and check boxes etc.
• Examples: &OK for OK or E&xit for Exit Activated by depressing Alt+O or Alt+x Try to follow the Windows defaults to avoid confusion
Setting the Default and Cancel Properties of Command Buttons
Set the Default property of one command button to True user can press Enter to select this button
Set the Cancel property to True for one command button this button will be selected when the user presses the Esc key.
Setting the Focus and Tab Order for Controls
One control on a a form always has the focus Focus is a light dotted line for command buttons and option
buttons Focus is the insertion point for text boxes image controls and labels do not have a focus The focus moves from control to control as the user presses the
Tab key. The sequence for tabbing is determined by the TabIndex properties
of the controls. Assigned by VB as you begin to enter controls Starts at 0 When program begins running, focus is on control with TabIndex 0 Although labels do not have a focus…they do have a tabindex
• If you use a & in the caption of a label, the focus will move to the first TabIndex following the label when the access key is activated,
Setting the Form’s Location on the Screen
The form will appear in the upper left hand corner of the screen by default To change – drag form in Form/Layout window or/ StartupPosition Property of Form set to 0-Left, 1-Right,2-Center
Creating ToolTips
Set the ToolTipText property of a control to make a ToolTip appear when the user pauses the mouse pointer over the control.
Coding for the Controls
controls can be changed at run time by using VB code in the event procedures
Text boxes and labels can be cleared The focus can be set Option buttons and check boxes can be preset Font and colors can be changed Multiple properties of a group of controls can be set
Clearing Text Boxes and Labels
Set the property to empty string or null string defined by “”• txtName.txt =“”
• lblAddress.Caption=“”
Resetting the Focus
To reset the focus, use Object.Method Example: txtName.SetFocus
Setting the Value Property of Option Buttons and Check Boxes
To set a default value to an option button or check box, use the Value Property
• Example: chkBold.Value = 1 means box is checked
• Example: chkBold.Value = 0 box is unchecked (Remember 2 is greyed or dimmed)
• Example: optRed.Value=True means button is selected
Changing the Font Properties of Controls
To change font attributes of a text box or a label, use the Font property of the control. The Font property refers to a Font object, with properties for bold, italic, underline, size, etc. Example: txtName.Font.Bold = True Example: txtName.Font.Size = 14 Example: txtName.Font.Underline= True
Changing the Color of Text
Change the color of text in a control by changing the ForeColor property txtMessage.ForeColor = vbGreen changes the text to Green
Change the color around the text by changing the BackColor property txtMessage.BackColor = vbWhite changes the area around the
text to White Several default Visual Basic color constants
vbBlack, vbWhite, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan
Changing Multiple Properties of a single Control during Run Time
Instead of : txtTitle.Visible=True txtTitle.Font.Bold = True txtTitle.ForeColor = vbRed txtTitle.SetFocus
Use a With block which is actually more efficient in terms of execution timeWith txtTitle
.Visible=True
.Font.Bold = True
.ForeColor = vbRed
.SetFocusEnd With
Concatenating Text
Joining two strings of text is called concatenation and is accomplished by placing & between the two elements. A space must precede and follow the & Example: lblMessage.Caption = “Your name is: “ & txtName.Text
Continuing Long Program Lines
Use a space and an underscore to continue a long statement on another line.
You may indent the next line You may not use this line continuation character in the middle
of a literal or split the name of an object or property Examples:
lblGreetings.Caption = “Greetings “ & txtName.Text & “:” & _
“You have been selected to win a free prize. “ & _
“Just send us $100 for postage and handling”
Using the Default Property of a Control
Using the default property of a control allows you to refer to an object without naming the property Example: A text box the default property is Text hence you can write
either
• txtCompany.Text = “ABC” or/
• txtCompany = “ABC”
• chkHours.Value = 1 or/
• chkHours =1 Table 2.1 in text provides a complete list
Top Related