Title: Chapter 11 Additional Controls and Ojects Outline and Objectives
1Chapter 11 Additional Controls and OjectsOutline
and Objectives
- List Boxes and Combo Boxes
- The List Box Control
- The Combo Box Control
- Nine Elementary Controls
- Frame, Check Box, Option Button, Horizontal
Scroll Bar, Vertical Scroll Bar, Timer, Shape,
Line, Image - Five Additional Objects
211.1 The List Box Control
Example 1 Private Sub cmdAdd_Click() Dim item
As String item InputBox("Item to Add")
lstOxys.AddItem item End Sub Private Sub
Form_Load() lstOxys.AddItem "jumbo shrimp"
lstOxys.AddItem "definite maybe"
lstOxys.AddItem "old news" lstOxys.AddItem
"good grief" End Sub
- Provides a list of items to the user
- The items on the list can either be specified at
design time or set with code in the Form-Load
event procedure - Items can be accessed, added, deleted from the
list by code - Use prefix lst for naming a List Box
3Useful Properties and Methods of List Box
- If Sorted property is set to true, the items will
automatically be displayed in alphabetic order. - AddItem method adds a string to the list.
- RemoveItem n statement deletes the item of index
n from the list. - ListCount property returns the number of items in
the list - Each item in the list is identified by an index
number, and ListIndex property is the index of
the item currently highlighted in list box.
4Adding and deleting items from the list Box
- Private Sub lstOxys_Click()
- picSelected.Cls
- picSelected.Print "The selected item is"
- picSelected.Print Chr(34) lstOxys.Text
Chr(34) "." - End Sub
- Private Sub cmdAdd_Click()
- Dim item As String
- item InputBox("Item to Add")
- lstOxys.AddItem item
- End Sub
- Private Sub LstOxys_DblClick()
- lstOxys.RemoveItem lstOxys.ListIndex
- End Sub
5Example 2, pp. 519uses NewIndex and ItemData to
provide data from a file
In the General Declaration Section Dim
inventor(0 To 10) as string Dim yr(0 To 10) as
integer
- Private Sub Form_Load()
- Dim what As String, who As String, when As
Integer, index As Integer - Open App.Path "\INVENTOR.TXT" For Input As 1
- index 0
- Do While (index lt UBound(inventor)) And (Not
EOF(1)) - Input 1, what, who, when
- index index 1
- lstInvents.AddItem what
- lstInvents.ItemData(lstInvents.NewIndex)
index - inventor(index) who
- yr(index) when
- Loop
- Close 1
- End Sub
Add the item to the list and record the index for
each record
6Example Continued
- Private Sub lstInvents_Click()
- lblWho.Caption inventor(lstInvents.ItemData(ls
tInvents.ListIndex)) - lblWhen.Caption Str(yr(lstInvents.ItemData(lst
Invents.ListIndex))) - End Sub
7The Combo Box Control
- A combo box is best thought of as a text box with
a help list attached. - The user has the option of either typing in
information or just selecting the appropriate
information from a list. - Two most useful types of combo box are
- Dropdown combo box
- Simple combo box
- Use prefix cbo for naming a combo box
8Style of Combo Boxes
Dropdown Combo Box
Simple Combo Box
9Example 3, pp. 521
- Private Sub cmdDisplay_Click()
- txtDisplay.Text cboTitle.Text " "
txtName.Text - End Sub
10Drive, Directory, and File List Box Controls
- There are three file-related controls
- DriveListBox, DirListBox, FileListBox
- VB does much of the work of providing appropriate
lists for the three boxes. - Windows determines the contents of the drive box.
- The programmer determines the contents of
directory list boxes and file list boxes with
Path properties. - Use prefixes drv, dir, and fil for the names of
the drive, directory, and file list box controls
respectively.
11Example 4display the full name of any file on
any drive
- Private Sub cmdDisplay_Click()
- picFileSpec.Cls
- picFileSpec.Print dirList.Path
- If Right(dirList.Path, 1) ltgt "\" Then
- picFileSpec.Print "\"
- End If
- picFileSpec.Print filList.FileName
- End Sub
- Private Sub dirList_Change()
- filList.Path dirList.Path
- End Sub
- Private Sub drvList_Change()
- dirList.Path drvList.Drive
- End Sub
1211.2 Nine Elementary Controls
- Frame
- Check Box
- Option Button
- Horizontal Scroll Bar
- Vertical Scroll Bar
- Timer
- Shape
- Image
13The Frame Control
- Used to group related set of controls
- Is a passive object
- Useful when working with group of controls
Frame
14The Check Box Control
- Provides a yes/no option to the user
- The value property of check box is 0 when the
check box is not checked and 1 if the check box
is checked - At run time, the user triggers the Click event by
checking and unchecking the box - Use prefix chk for naming a Check box
15Example of Using Check Box
- Private Sub Tally()
- Dim sum As Single
- If chkDrugs.Value 1 Then
- sum sum 12.51
- End If
- If chkMedical.Value 1 Then
- sum sum 25.25
- End If
- lblAmount.Caption FormatCurrency(sum)
- End Sub
16The Option Button Control
- Used to give the user a single choice from
several options. - Only one option button in a group can be on at
the same time. - The value property of an option button tells if
the button is on or off. - Use prefix opt for naming option buttons
17Example of Option Button Control
- Private Sub cmdStatus_Click()
- picStatus.Cls
- If optOpt1.Value True Then
- picStatus.Print "Option1 is on.
- End If
- If optOpt2.Value True Then
- picStatus.Print "Option2 is on."
- End If
- End Sub
- Private Sub Form_Load()
- optOpt1 False 'Turn off optOpt1
- optOpt2 False 'Turn off optOpt2
- End Sub
18Another Example of Option Button Control
- Private Sub opt12pt_Click()
- txtInfo.Font.Size 12
- End Sub
- Private Sub opt18pt_Click()
- txtInfo.Font.Size 18
- End Sub
- Private Sub opt24pt_Click()
- txtInfo.Font.Size 24
- End Sub
19The Horizontal and Vertical Scroll Bar Controls
- The main properties of a scroll bar are Min, Max,
Value, SmallChange, and LargeChange, which are
set to integers. - Use prefix hsb for naming horizontal scroll bar
- Use prefix vsb for naming Vertical scroll bar
20Example 5, pp. 534uses scroll bars to move a
smiling face around the form
- Private Sub hsbXPos_Change()
- lblFace.Left hsbXPos.Value
- End Sub
- Private Sub hsbXPos_Scroll()
- lblFace.Left hsbXPos.Value
- End Sub
- Private Sub vsbYPos_Change()
- lblFace.Top vsbYPos.Value
- End Sub
- Private Sub vsbYPos_Scroll()
- Let lblFace.Top vsbYPos.Value
- End Sub
21The Timer Control
- The timer control, is invisible during run time.
- It triggers an event after a specified amount of
time. - The length of time is measured in milliseconds
- The event triggered each time Timer1.Interval
milliseconds elapses is called Timer1.Timer(). - To begin a timer, set its Enabled property to
True. - To stop a timer, set its Enabled property to
False or set its Interval to 0. - Use prefix tmr for naming Timer control
22Example 6create a stopwatch that updates the
time every tenth of a second
- Private Sub cmdStart_Click()
- lblTime.Caption " 0" 'Reset watch
- tmrWatch.Enabled True
- End Sub
- Private Sub cmdStop_Click()
- tmrWatch.Enabled False
- End Sub
- Private Sub tmrWatch_Timer()
- lblTime.Caption Str(Val(lblTime.Caption)
0.1) - End Sub
23The Shape Control
- The shape control assumes one of six possible
predefined shapes depending on the value of its
shape property. - Most useful properties are BackStyle,
BorderWidth, BorderStyle, BackColor, FillStyle,
FillColor, and Visible. - Use prefix shp for naming shape controls
Effect achieved with Shape control
24The Line Control
- The Line control, produces lines of various
thickness, styles, and colors to enhance the
visual appearance of the forms. - Most useful properties are BorderColor,
BorderWidth, BorderStyle, and Visible. - Use prefix lin for naming Line controls
25The Image Control
- It holds pictures stored in graphics files.
- Pictures are placed in image controls with the
Picture property. - If the Stretch property is set to True, the
pictures will be resized to fit the image
control. - A picture can be assigned to an image control at
run time by using the LoadPicture function. - Use prefix img for naming Image controls
2611.3 Five Additional Objects
- Microsoft FlexGrid Control
- Menu Control
- Common Dialog Control
- Clipboard
- Multiple Forms
27The Microsoft FlexGrid Control
- A grid is a rectangle arrray used to display
tables or to create spreadsheet-like application, - To add the grid control, click on Components in
the project menu, click on the Controls tab, and
check the Microsoft FlexGrid Control 6.0. Then
press OK button. - Use prefix msg for naming FlexGrid controls
28The Microsoft FlexGrid Control
- The number of rows or columns can be specified at
design time with Rows and Column properties or at
run time - The width of each column can be specified only at
run time with the ColWidth property. - The individual rectangles are called cells.
- At any time, one cell is singled out as the
current cell. - The statement msgFlex.Text str places the value
of the str in the current cell
29The Menu Control
- VB forms can have menu bars with sub menus and
dropdown menus at different levels. - Each menu item is treated as a distinct control
that responds to only one event - the click
event. - Menus are created with the Menu Editor window
available from the Tools menu. - Use prefix mnu for naming Menu controls
30Example 3, pp. 551 a menu is used to alter the
appearance of the contents of a text box
- Private Sub mnu12_Click()
- txtInfo.Font.Size 12
- End Sub
- Private Sub mnu24_Click()
- txtInfo.Font.Size 24
- End Sub
- Private Sub mnuCourier_Click()
- txtInfo.Font.Name "Courier"
- End Sub
- Private Sub mnuTimesRm_Click()
- txtInfo.Font.Name "Times New Roman"
- End Sub
31The Clipboard Object
- The clipboard object is used to copy or move text
from one location to another. - It can be used to transfer information from one
Windows application to another. - If str is a string, then the statement
Clipboard.SetText str replaces any text currently
in the clipboard with str. - The statement strClipboard,GetText() assigns the
text currently to the string variable str.
32Multiple Forms
- A VB program can contain more than one form.
- Additional forms are created from the Project
menu with Add Form. - The name of each form appears in the Project
Explorer window. - Forms are hidden or activated with statement such
as Form1.Hide or Form2.Show. - Each form has its own controls and code.
33Example 4, pp. 553uses a second form as a dialog
box to total the different source of income
- Private Sub cmdShowTot_Click()
- frmSources.Show vbModal
- End Sub
- Private Sub cmdCompute_Click()
- Dim sum As Single
- sum Val(txtWages.Text) Val(txtInterest.Text)
Val(txtDividend.Text) - frmIncome.txtTotal.Text FormatCurrency(Str(sum
)) - frmSources.Hide
- End Sub
34Multiple Forms
- You can declare global variables and procedures
that are available to all forms. - To do so, select Add Module from the Project menu
and then double-click on the Module icon. - Procedures created in this code window with the
Public keyword will be available to all forms. - To declare a variable that is available to all
forms, declare it in Module1 window, but use the
keyword Public instead of Dim. - Save the code module by choosing Save Module As
from the File menu.
35The Common Dialog Control
- To add a Common Dialog control, select Components
from the Project menu, click the Controls tab,
and click on the check box for Microsoft Common
Dialog Control 6.0. Then press OK button. - Use prefix dlg for naming Common Dialog controls.
- The Common Dialog control has no events, only
methods and properties. - It is also invisible. To produce a common dialog
box, you execute a statement such as dlg.ShowFont
36The Common Dialog Control
- The different types of dialog boxes
37Examplea program to select the color and the
font for the text box using dialog boxes
- Private Sub cmdColor_Click()
- dlgStyle.Flags 3
- dlgStyle.Action 3
- txtInfo.ForeColor dlgStyle.Color
- End Sub
- Private Sub cmdFont_Click()
- dlgStyle.Flags 3
- dlgStyle.ShowFont
- txtInfo.Font.Name dlgStyle.FontName
- txtInfo.Font.Bold dlgStyle.FontBold
- txtInfo.Font.Italic dlgStyle.FontItalic
- txtInfo.Font.Size dlgStyle.FontSize
- End Sub
Select the Color common dialog box
Select the Font common dialog box