<<

CHAPTER 10 BASIC COMPONENTS 1

Chapter 10 Basic Graphical User Interface Concepts (Main Page)

10.1 Intrinsic controls. 10.2 Components dialog. 10.3 ActiveX controls discussed in this book. 10.4 Database ActiveX Controls presented in Chapter 18. 10.5 Common TextBox properties and methods. 10.6 Program that demonstrates using TextBoxes controls. 10.7 MaskEdit control in the Components dialog. 10.8 Pop-up displayed when a MaskEdit control is clicked with the right mouse . 10.9 Property Pages dialog for the MaskEdit control. 10.10 Some MaskEdit control properties. 10.11 Some common mask characters. 10.12 MaskEdit control at design time. 10.13 Demonstrating the MaskEdit control. 10.14 Property Pages dialog for MaskEdit control of Fig. 10.12 and 10.13. 10.15 Common ComboBox properties methods and events. 10.16 ComboBox styles. 10.17 Demonstrating the ComboBox control. 10.18 Common ListBox properties and methods. 10.19 ListBox styles. 10.20 Multiple selection in a ListBox. 10.21 MultiSelect property values. 10.22 Demonstrating the ListBox control. 10.23 HScrollBar control. 10.24 Common scrollbar properties and methods. 10.25 Demonstrating scrollbars. 10.26 Sliders. 10.27 Common properties and methods. 10.28 TickStyle property values. 10.29 Demonstrating the Slider control. 10.30 Menu Editor dialog. 10.31 An expanded menu. 10.32 Checked menu items. 10.33 Program with two menus. 10.34 Menu Editor dialog for the program of Fig. 10.33. 10.35 Demonstrating pop-up menus. 10.36 MsgBox dialog generated by Visual Basic. 10.37 MsgBox constants for specifying the number and type of buttons. 10.38 MsgBox constants for icons. 10.39 MsgBox constants for specifying the number and type of buttons. 10.40 MsgBox return values. 10.41 Demonstrating the function MsgBox. 10.42 MsgBox constants for specifying the number and type of buttons.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 2

Pre- ferred Control Prefix Description

CheckBox chk Acts as a toggle. (Ch. 7)

ComboBox cbo Displays a drop-down list. (Ch. 10)

Data dat Navigates data in a database.

DirListBox dir Displays a directory box. (Ch. 14)

DriveListBox drv Displays a drive . (Ch. 14)

FileListBox fil Displays a file list box. (Ch. 14)

Frame fra Acts as a container for other controls. (Ch. 6) Image img Displays images. (Ch. 9)

Label lbl Displays static text. (Ch. 2)

Line lin Draws lines. (Ch. 9)

ListBox lst Displays a list. (Ch. 10)

OLE ole Acts as an OLE container.

OptionButton opt Displays options from which the user can choose only one. (Ch. 10) PictureBox pic Displays images. Also acts as a container for other controls. (Ch. 9) scrollbars hsb, vsb Specify a range of values. (Ch. 10)

Shape shp Draws graphical shapes. (Ch. 9)

TextBox txt Control for user input. (Ch. 10)

Timer tmr Raises an event after a specified interval. (Ch. 9)

Fig. 10.1 Intrinsic controls.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 3

Fig. 10.2 Components dialog.

Pre- ferred Control Icon Prefix Description

CommonDialog dlg Provides several common dialog boxes such as Open, Save, etc. (Ch. 9) FlatScrollBar fsb A scrollbar that provides a visual response when the mouse is positioned over the scrollbar. (Ch. 11) ImageCombo imgcbo An enhanced ComboBox that is capable of displaying images and text. (Ch. 11)

ImageList ils Stores a series of images. (Ch. 11)

MaskedEdit msk An enhanced TextBox that can control the format of text entered by the user. (Ch. 10) RichTextBox rtf An enhanced TextBox that provides basic text editor capabilities. (Ch. 11) Slider sld Represents a range of values. (Ch. 10)

UpDown upd Allows scrolling through a set of values. (Ch. 11)

Fig. 10.3 Some ActiveX controls discussed in this book.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 4

Pre- ferred Control Icon Prefix Description

ADO Data ado Supports connecting to a database. Control DataCombo dbc An enhanced ComboBox that is popu- lated automatically with data from a data- base. DataGrid dgd A table that is populated automatically with data from a database. DataList dbl An enhanced ListBox that is populated automatically with data from a database. Hierarchical flex A table that is populated automatically FlexGrid with data from a database. This table is read-only (data cannot be modified).

Fig. 10.4 Database ActiveX Controls presented in Chapter 18.

Property/method Description

Properties Enabled Specifies whether or not the user can interact with the control. Index Specifies the control array index. Locked Boolean. Specifies whether or not the user can type in the TextBox. MaxLength Specifies the maximum number of characters input. Default of 0 corre- sponds to any number of characters. MousePointer Specifies the shape of the mouse pointer when over a TextBox. Multiline Specifies whether or not the TextBox contains more than one line. PasswordChar Specifies the masking character for text displayed in the TextBox. Scrollbars Specifies either horizontal scrollbars, vertical scrollbars or both horizontal and vertical scrollbars for the TextBox. This property is used with the Multiline property. Text Specifies the TextBox text. ToolTipText Specifies what text is displayed as the TextBox’s tool tip. Visible Specifies whether or not the TextBox is visible.

Method SetFocus Transfers focus to the TextBox.

Event Procedures Change Called when text in TextBox changes. Clicked Called when TextBox is clicked with the mouse. GotFocus Called when TextBox receives the active focus. LostFocus Called when TextBox loses the active focus.

Fig. 10.5 Common TextBox properties and methods.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 5

Property/method Description

KeyDown Called when a key is pressed while the TextBox has the focus. KeyUp Called when a key is released while the TextBox has the focus.

Fig. 10.5 Common TextBox properties and methods.

1 ' Fig. 10.6 2 ' Demonstrating the TextBox Control 3 Option Explicit ' General declaration 4 5 Private Sub txtInput_Change() 6 txtDisplay.Text = txtInput.Text ' Copy to other TextBox 7 End Sub

tool tip

mouse pointer

Fig. 10.6 Program that demonstrates using TextBox controls.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 6

Fig. 10.7 MaskEdit control in the Components dialog.

Selected MaskEdit Activex control

Pop-up menu

Properties menu item that displays Property Pages dialog

Fig. 10.8 Pop-up menu displayed when a MaskEdit control is clicked with the right mouse button.

Tabs

Fig. 10.9 Property Pages dialog for the MaskEdit control.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 7

Property Description

AllowPrompt Specifies whether or not the prompt character is acceptable input. AutoTab Specifies whether or not the focus is transferred to the contol in the order when the required number of characters is input. Enabled Specifies whether or not the user is allowed to interact with the con- trol. Format The format the text is displayed in when the control does not have the focus. HideSelecton Specifies whether or not the selected text appears highlighted when the control loses focus. Mask A String that describes the input mask. Masking characters are shown in Fig. 10.11. MaxLength Maximum number of characters allowed as input. MousePointer Specifies the shape of the mouse pointer. Text Text displayed in control.

Fig. 10.10 Some MaskEdit control properties.

Mask character Description

# A numeric character is required in this position. . A decimal point which is treated as a literal (a character that is simply displayed in the MaskEdit control. , A thousands separator which is treated as a literal. / A date separator which is treated as a literal. : A time separator which is treated as a literal. \ The character immediately following the backslash is treated as a lit- eral. Backslash is often used with the other mask symbols: a, A, etc. & An ANSI character in the ranges 32 to 126 and 128 to 255 (see Appen- dix B for a complete listing of the ANSI character set). > Converts characters to uppercase. < Converts characters to lowercase. A An alphanumeric character is required at this position. a An alphanumeric character is optional at this position. 9 A numeric character is optional at this position. ? An alpha character is required in this position.

Fig. 10.11 Some common mask characters.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 8

Fig. 10.12 MaskEdit control at design time.

Initial form startup. Button has the focus.

Focus transferred to the MaskEdit control when the Tab key is pressed.

MaskEdit control only accepts numeric input. The user can- not modify the paren- theses or hyphen.

As soon as the last digit is input, the focus is transferred to the Dial button.

Fig. 10.13 Demonstrating the MaskEdit control.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 9

Fig. 10.14 Property Pages dialog for MaskEdit control of Figs. 10.12 and10.13.

Property/method Description

Properties Enabled Boolean. Specifies if the user can interact with the ComboBox. Index Integer. Specifies the control array index. List String array. Contains the Strings displayed in the drop-down list. Starting array index is 0. ListCount Integer. Contains the number of drop-down list items. ListIndex Integer. Contains the index of the selected ComboBox item. If an item is not selected, ListIndex is -1. Locked Boolean. Specifies whether or not the user can type data in the ComboBox. MousePointer Integer. Specifies the shape of the mouse pointer when over the area of the ComboBox. NewIndex Integer. Index of last item added to the ComboBox. If the Com- boBox does not contain any items, NewIndex is -1. Sorted Boolean. Specifies whether or not a ComboBox’s items are sorted. Style Integer. Specifies the ComboBox appearance. (See Fig. 10.16.) TabStop Boolean. Determines whether or not the ComboBox receives the focus. Text String. Specifies the ComboBox’s selected item. ToolTipText String. Specifies what text is displayed as the ComboBox‘s tool tip. Visible Boolean. Specifies whether or not the ComboBox is visible. Methods AddItem Adds an item to the ComboBox. Clear Removes all items from the ComboBox.

Fig. 10.15 Common ComboBox properties, methods and events (part 1 of 2).

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 10

Property/method Description

RemoveItem Removes the specified item from the ComboBox. SetFocus Transfers focus to the ComboBox. Event Procedures Change Called when text in ComboBox changes. DropDown Called when the ComboBox drop-down list is displayed. GotFocus Called when ComboBox receives the focus. LostFocus Called when ComboBox loses the focus.

Fig. 10.15 Common ComboBox properties, methods and events (part 2 of 2).

Fig. 10.16 ComboBox styles.

1 ' Fig. 10.17 2 ' Demonstrating the ComboBox control. 3 Option Explicit ' General declaration 4 5 Private Sub cmdInput_Click() 6 7 ' Add item to cboCombo drop-down list 8 Call cboCombo.AddItem(txtInput.Text) 9 10 ' Display input in cboCombo's text area 11 cboCombo.Text = txtInput.Text 12 13 Call UpdateLabel ' Display stats 14 txtInput.Text = "" ' Clear user input 15 End Sub 16 17 Private Sub UpdateLabel() ' Programmer defined procedure 18 lblStats.Caption = "ListCount: " & cboCombo.ListCount & _ 19 " ListIndex: " & cboCombo.ListIndex & _ 20 " New Index: " & cboCombo.NewIndex 21 End Sub 22 23 Private Sub cboCombo_LostFocus() 24 25 ' When cboCombo loses focus call updateLabel 26 Call UpdateLabel 27 End Sub 28 29 Private Sub cmdClear_Click() 30 cboCombo.Clear ' Clear all items 31 Call UpdateLabel ' Display stats 32 End Sub

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 11

33 34 Private Sub cmdRemove_Click() 35 36 ' Remove item from cboCombo 37 Call cboCombo.RemoveItem(cboCombo.ListIndex) 38 39 ' Display cboCombo item 0 40 cboCombo.Text = cboCombo.List(0) 41 End Sub

Fig. 10.17 Demonstrating the ComboBox control (part 1 of 3).

Text area of ComboBox GUI displayed when program is executed.

Clicking the down User entering text in the arrow displays the TextBox. drop-down list.

TextBox is cleared when user presses Add. TextBox text is added. to ComboBox.

GUI after the user has Drop- entered five items. down list Drop-down list displays the five items input.

Fig. 10.17 Demonstrating the ComboBox control (part 2 of 3).

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 12

GUI displayed after the user pressed Clear. Drop-down list indi- cates ComboBox is empty.

Fig. 10.17 Demonstrating the ComboBox control (part 3 of 3).

Property/method Description

Properties Columns Integer. Specifies whether or not ListBox contents are listed ver- tically. When 0, items are listed vertically. A value greater than 0 specifies the number of columns in which items are listed horizontally. Enabled Boolean. Specifies if the user can interact with the ListBox. Index Integer. Specifies the control array index. List String array. Contains the Strings displayed in the ListBox. Starting array index is 0. ListCount Integer. Contains the number of ListBox items. ListIndex Integer. Contains the index of the selected ListBox item. If an item is not selected, ListIndex is -1. MousePointer Integer. Specifies the shape of the mouse pointer when over the area of the ListBox. MultiSelect Integer. Specifies whether or not the user can select more than one item at a time from the ListBox. (See Fig. 10.20.) NewIndex Integer. Index of last item added to the ListBox. If the ListBox does not contain any items, NewIndex is -1. SelCount Integer. Contains the number of ListBox items selected. Sorted Boolean. Specifies whether or not a ListBox’s items are sorted. Style Integer. Specifies the ListBox appearance. (See Fig. 10.19.) TabStop Boolean. Determines whether or not the ListBox receives the focus. Text String. Specifies the TextBox’s selected item. ToolTipText String. Specifies what text is displayed as the ListBox‘s tool tip. Visible Boolean. Specifies whether or not the ListBox is visible. Methods AddItem Adds an item to the ListBox. Clear Removes all items from the ListBox. RemoveItem Removes one item from the ListBox. SetFocus Transfers focus to the ListBox. Event Procedures Click Called when the user clicks once on a ListBox item.

Fig. 10.18 Common ListBox properties and methods.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 13

Property/method Description

DblClick Called when the user double clicks on a ListBox item. GotFocus Called when ListBox receives the focus. LostFocus Called when ListBox loses the focus.

Fig. 10.18 Common ListBox properties and methods.

Fig. 10.19 ListBox styles.

Fig. 10.20 Multiple selection in a ListBox.

Value Description

0 (None) ListBox does not allow multiple selections. 1 (Simple) ListBox allows multiple selections. Items are selected/deselected with either the mouse or the Space bar. 2 (Extended) ListBox allows multiple selections. Items are selected/deselected with either the mouse or the Space bar. Holding the Shift key and selecting an item selects all items between the last selection and the current selection. The arrow keys can also be used with Shift. Holding the Ctrl key and selecting an item selects/deselects that item.

Fig. 10.21 MultiSelect property values.

1 ' Fig. 10.22 2 ' Demonstrating the ListBox control 3 Option Explicit ' General declaration 4 5 Private Sub cmdAdd_Click() 6 Call lstList.AddItem(txtInput.Text) ' Add Text to ListBox 7 txtInput.Text = "" ' Clear TextBox 8 End Sub 9 10 Private Sub cmdRemove_Click() 11 12 ' If an item is selected then delete it 13 If lstList.ListIndex <> -1 Then 14 Call lstList.RemoveItem(lstList.ListIndex)

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 14

15 End If 16 17 End Sub 18 19 Private Sub cmdClear_Click() 20 Call lstList.Clear ' Remove all list items 21 End Sub 22 23 Private Sub cmdExit_Click() 24 End ' Terminate execution 25 End Sub

Initial GUI at execution. The lines on the face of the buttons indicate access keys. Alt + A is equivalent to pressing Add, Alt + R is equivalent to pressing Remove, etc.

Fig. 10.22 Demonstrating the ListBox control (part 1 of 3).

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 15

GUI after two items have been added to the ListBox. User is inputting lion.

GUI after many items have been added to the ListBox. Each item is added in alpha- betical order. When more items exist than can be dis- played in ListBox, a scrollbar automatically appears.

Scrollbar

Selected item. Dot- ted border indicates this is the text area. User selecting lion with the mouse.

Fig. 10.22 Demonstrating the ListBox control (part 2 of 3).

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 16

GUI after Remove button is pressed. lion is removed from the ListBox.

GUI after Clear button is pressed. Scrollbar disappears automatically.

Fig. 10.22 Demonstrating the ListBox control (part 3 of 3).

Scroll Arrow Minimum Value Maximum Value Scroll Arrow

Scroll Box

Fig. 10.23 HScrollBar control.

Property/method Description

Properties Enabled Boolean. Specifies whether or not the user is allowed to interact with the scrollbar. Index Integer. Specifies the control array index. LargeChange Integer. Amount the property Value changes by when the area between the scroll box and scroll arrow is clicked. Max Integer. The largest value that property Value can have.

Min Integer. The smallest value that property Value can have. MousePointer Integer. Specifies the shape of the mouse pointer when over the area of the scrollbar. SmallChange Integer. Amount the property Value changes by when a scroll arrow is pressed.

Fig. 10.24 Common scrollbar properties and methods (part 1 of 2).

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 17

Property/method Description

TabStop Boolean. Determines whether or not the scrollbar receives the focus. Value Integer. The position of the scroll box. Visible Boolean. Specifies whether or not the scrollbar is visible.

Method SetFocus Transfers focus to the scrollbar.

Event Procedures Change Called when the scrollbar Value property changes (i.e., the scroll box has moved). GotFocus Called when scrollbar receives the focus. LostFocus Called when scrollbar loses the focus. Scroll Called repeatedly when the scroll box is clicked and dragged (clicking the scroll box and holding the mouse button, then moving the mouse).

Fig. 10.24 Common scrollbar properties and methods (part 2 of 2).

1 ' Fig. 10.25 2 ' Demonstrating Scrollbars 3 Option Explicit ' General declarations 4 5 Private Sub Form_Load() 6 ' Set maximum values for scrollbars 7 hsbScroll.Max = picPicture.Width 8 vsbScroll.Max = picPicture.Height 9 End Sub 10 11 Private Sub hsbScroll_Change() 12 ' Turn pixel on when horizontal scrollbar value changes 13 picPicture.PSet (hsbScroll.Value, vsbScroll.Value) 14 End Sub

Fig. 10.25 Demonstrating scrollbars (part 1 of 2).

15 16 Private Sub vsbScroll_Change() 17 ' Turn pixel on when vertical scrollbar value changes 18 picPicture.PSet (hsbScroll.Value, vsbScroll.Value) 19 End Sub 20 21 Private Sub hsbScoll_Scroll() 22 picPicture.PSet (hsbScroll.Value, vsbScroll.Value) 23 End Sub 24 25 Private Sub vsbScroll_Scroll() 26 picPicture.PSet (hsbScroll.Value, vsbScroll.Value) 27 End Sub

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 18

Initial GUI at execution.

GUI after the user has drawn a picture using the scrollbars.

Fig. 10.25 Demonstrating scrollbars (part 2 of 2).

Thumb

Slider tick marks

(a) (b) (c) (d)

Fig. 10.26 Sliders.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 19

Property/method Description

Properties Enabled Boolean. Specifies whether or not the user is allowed to interact with the Slider. GetNumTicks Long. Contains the number of ticks on the Slider. Index Integer. Specifies the control array index. LargeChange Integer. Amount the property Value changes by when the area between the thumb and the end/beginning of the Slider is clicked. Max Integer. The largest value that property Value can have. Min Integer. The smallest value that property Value can have. MousePointer Integer. Specifies the shape of the mouse pointer when over the Slider. Orientation Integer. Specifies whether or not Slider is horizontal or vertical. SmallChange Integer. Amount the property Value changes by when the Slider is clicked. TabStop Boolean. Determines whether or not the Slider receives the focus. TickFrequency Long. Specifies the number of units between each tick mark. TickStyle Integer. Describes if and where tick marks are displayed. (See Fig. 10.28.) Value Integer. The position of the thumb. Visible Boolean. Specifies whether or not the Slider is visible.

Method SetFocus Transfers focus to the Slider.

Event Procedures Change Called when the Slider Value property changes (i.e., moving the thumb, or assigning a number to Value). GotFocus Called when Slider receives the focus. LostFocus Called when Slider loses the focus. Scroll Called when the thumb is clicked-and-dragged (clicking the thumb and holding the mouse button, then moving the mouse).

Fig. 10.27 Common Slider properties and methods.

Val- Constant ue Description

sldBottomRight 0 Ticks are displayed by default on the bottom of horizontal Sliders and to the right of vertical Sliders (Fig. 10.26a). sldTopLeft 1 Ticks are displayed on top of horizontal Sliders and to the left of vertical Sliders (Fig. 10.26b).

Fig. 10.28 TickStyle property values (part 1 of 2).

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 20

Val- Constant ue Description

sldBoth 2 Ticks are displayed on both sides of the Slider (Fig. 10.26d). sldNoTicks 3 Ticks are not displayed (Fig. 10.26c).

Fig. 10.28 TickStyle property values (part 2 of 2).

1 ' Fig. 10.29 2 ' Demonstrating the Slider control 3 Option Explicit ' General declaration 4 5 Private Sub sldVolume_Change() 6 lblDb.Caption = sldVolume.Value & " dB" 7 End Sub 8 9 Private Sub sldVolume_Scroll() 10 lblDb.Caption = sldVolume.Value & " dB" 11 End Sub

Initial GUI at execution.

User dragging the thumb. The Value of the Slider is dis- played above the thumb (TextPosi- tion property).

Fig. 10.29 Demonstrating the Slider control.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 21

Fig. 10.30 Menu Editor dialog.

Top-level menus

Command Grayed or disabled Shortcut key command

Separator bar Arrowhead indicates a submenu

Fig. 10.31 An expanded menu.

Check menu items (three checked and one unchecked)

Fig. 10.32 Checked menu items.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 22

Initial GUI at execution.

User selecting .

User selecting Special sub- menu of Graphics.

Fig. 10.33 Program with two menus.

Separator bar

Shortcut keys

Fig. 10.34 Menu Editor dialog for the program of Fig. 10.33.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 23

1 ' Fig. 10.35 2 ' Demonstrating pop-up menus 3 Option Explicit ' General Declaration 4 5 Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _ 6 X As Single, Y As Single) 7 8 ' When right button is clicked display pop-up menu 9 If Button = vbRightButton Then 10 Call PopupMenu(mnuPopUp) 11 End If 12 13 End Sub 14 15 Private Sub mnuitmLeft_Click() 16 lblText.Alignment = vbLeftJustify 17 End Sub 18 19 Private Sub mnuitmRight_Click() 20 lblText.Alignment = vbRightJustify 21 End Sub 22 23 Private Sub mnuitmCenter_Click() 24 lblText.Alignment = vbCenter 25 End Sub

Initial GUI at execution.

Pop-up menu displayed when user right-clicks on the form.

Fig. 10.35 Demonstrating pop-up menus (part 1 of 2).

Title bar Message

Icon

Buttons

Fig. 10.36 MsgBox dialog generated by Visual Basic.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 24

Val- Constant ue Description

vbOKOnly 0 MsgBox displays OK button. vbOKCancel 1 MsgBox displays OK and Cancel buttons. vbAbortRetryIgnore 2 MsgBox displays Abort, Retry and Ignore buttons. vbYesNoCancel 3 MsgBox displays Yes, No and Cancel buttons. vbYesNo 4 MsgBox displays Yes and No buttons. vbRetryCancel 5 MsgBox displays Retry and Cancel buttons.

Fig. 10.37 MsgBox constants for specifying the number and type of buttons.

Val- Constant ue Icon Description

vbCritical 16 When serious errors or problems occur, the critical icon is typically displayed. Displays critical icon. vbQuestion 32 When the user must make a decision, the question mark icon is typically dis- played. Displays question mark icon. vbExclamation 48 Displays exclamation mark icon. Typi- cally used to draw the user’s attention to the message being displayed. vbInformation 64 Displays information icon. Typically used to give the user information about an action.

Fig. 10.38 MsgBox constants for icons.

Val- Constant ue Description

vbApplicationModal 0 A MsgBox is modal—the user cannot interact with other windows in the same application until the MsgBox is dismissed. For “application modal” MsgBoxes, the user ca still interact with other programs. vbSystemModal 4096 A MsgBox is modal—the user cannot interact with other windows in the same application until the MsgBox is dismissed. For “system modal” MsgBoxes, the user also cannot interact with other programs until the Msg- Box is dismissed.

Fig. 10.39 MsgBox constants for specifying the number and type of buttons.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 25

Val- Constant ue Description

vbOK 1 OK button was pressed. vbCancel 2 Cancel button was pressed. vbAbort 3 Abort button was pressed. vbRetry 4 Retry button was pressed. vbIgnore 5 Ignore button was pressed. vbYes 6 Yes button was pressed. vbNo 7 No button was pressed.

Fig. 10.40 MsgBox return values.

1 ' Fig. 10.41 2 ' Demonstrating Function MsgBox 3 Option Explicit ' General declaration 4 Dim mButtonType As Integer ' General declaration 5 Dim mButtonIcon As Integer ' General declaration 6 Dim mModal As Integer ' General declaration 7 8 Private Sub Form_Load() 9 mButtonType = vbOKOnly 10 mButtonIcon = vbCritical 11 mModal = vbApplicationModal 12 13 optType(mButtonType).Value = True 14 optIcon(mButtonIcon).Value = True 15 optModal(mModal).Value = True 16 End Sub 17 18 Private Sub cmdDisplay_Click() 19 Dim r As Integer 20 21 r = MsgBox("Visual Basic 6 How To Program", _ 22 mButtonType + mButtonIcon + mModal, "VBHTP") 23 24 ' Determine which MsgBox button was pressed 25 Select Case r 26 Case vbOK 27 lblDisplay.Caption = "OK was pressed." 28 Case vbCancel 29 lblDisplay.Caption = "Cancel was pressed." 30 Case vbAbort 31 lblDisplay.Caption = "Abort was pressed." 32 Case vbRetry 33 lblDisplay.Caption = "Retry was pressed." 34 Case vbIgnore 35 lblDisplay.Caption = "Ignore was pressed."

Fig. 10.41 Demonstrating function MsgBox (part 1 of 3).

36 Case vbYes 37 lblDisplay.Caption = "Yes was pressed." 38 Case vbNo 39 lblDisplay.Caption = "No was pressed." 40 End Select 41

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 26

42 End Sub 43 44 Private Sub optIcon_Click(Index As Integer) 45 mButtonIcon = Index 46 End Sub 47 48 Private Sub optType_Click(Index As Integer) 49 mButtonType = Index 50 End Sub 51 52 Private Sub optModal_Click(Index As Integer) 53 mModal = Index 54 End Sub

Initial GUI at execution.

MsgBox dialog displayed after the user selects vbYesNo, vbExclaimation and vbApplicationModal and presses Display.

Fig. 10.41 Demonstrating function MsgBox (part 2 of 3).

GUI after user dismisses MsgBox by pressing Yes.

Fig. 10.41 Demonstrating function MsgBox (part 3 of 3).

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook. CHAPTER 10 BASIC GRAPHICAL USER INTERFACE COMPONENTS 27

Val- Constant ue Description

vbDefaultButton1 0 MsgBox’s first button (i.e., the leftmost one) gets the focus. vbDefaultButton2 256 MsgBox’s second button gets the focus. vbDefaultButton3 512 MsgBox’s third button gets the focus. vbDefaultButton4 768 MsgBox’s fourth button gets the focus.

Fig. 10.42 MsgBox constants for specifying the number and type of buttons.

© Copyright 1999 Prentice Hall. All Rights Reserved. For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.