17
List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

Embed Size (px)

Citation preview

Page 1: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

List box & Combo box controls

School of BusinessEastern Illinois University

© Abdou Illia, Spring 2003

(Week 14, Monday 4/21/2003)

Page 2: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

2List box, Combo box ?

Pointer

Label

Frame

Check Box

Combo Box

Horizontal Scroll Bar

Timer

Directory List Box

Shapes

Image Box

Object Linking Embedding

Picture Box

Text Box

Command Button

Option Button

List Box

Vertical Scroll Bar

Drive List Box

File List Box

Lines

Data Tool

The Toolbox

List box with scroll bar

List box without scroll bar

Dropdown Combo box

Simple Combo box

Page 3: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

3Typical use of List and Combo boxes

Page 4: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

4List box control

Displays a list of items from which the user can select one or more.

If the number of items exceeds the number that can be displayed, a scroll bar is automatically added

Common properties:– List Array of items in the list box (Note: index=0 through n)– Sorted True or False (items in alphabetical order)– Multiselect (0, 1 or 2 for No-multiple, Simple-Multiple or

Multiple selection)– Text Returns item currently highlighted

Examples: lstEmployee.Multiselect = 2 lstEmployee.Sorted = True lstEmployee.List(0) = “John” picOutput.Print lstEmployee.Text

Page 5: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

5List box control

Common Methods:– AddItem item Adds the item referred to as item to the list– RemoveItem n Removes the item of index n from the list box– Clear Deletes all items in the list box– ListCount Returns the # of items in the list box– ListIndex Returns the index of the item currently

highlighted in the list box. – NewIndex Returns the index of the most recently added

item

Examples: – lstEmployee.AddItem “John”– lstEmployee.RemoveItem 2– lstEmployee.Clear– NumberOfEmployees = lstEmployee.ListCount– picOutput.Print lstEmployee.NewIndex

Page 6: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

6

Private Sub Form_Load() lstEmployee.AddItem "Barbara Lewis" lstEmployee.AddItem "John Whashington" lstEmployee.AddItem "Tom Davis" lstEmployee.AddItem "Nathalie Melrose"End Sub

Private Sub cmdAdd_Click() Dim item As String item = InputBox("Employee name be to Add:") lstEmployee.AddItem itemEnd Sub

Private Sub lstEmployee_Click() picSelected.Cls picSelected.Print "The selected Employee is" picSelected.Print Chr(34) & lstEmployee.Text & Chr(34) & "."End Sub

Private Sub LstEmployee_DblClick() lstEmployee.RemoveItem lstEmployee.ListIndexEnd Sub

List box: Example

Page 7: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

7List box: Exercise

Suppose that the list box lstEmployee is as shown and determine the effect of the code. (Assume the Sorted property is set to True)

picOutput.Print lstEmployee.Text

Answer:

picOutput.Print lstEmployee.List(2)

Answer:

lstEmployee.AddItem “Dan Kelli”

Answer:

lstEmployee.RemoveItem 0

Answer:

Page 8: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

8List box: Exercise

Suppose that the list box lstEmployee is as shown and determine the effect of the code. (Assume the Sorted property is set to True)

lstEmployee.AddItem “Dan Kelli”picOutput.Print lstEmployee.NewIndex

Answer:

For n = 0 To lstEmployee.ListCount – 1 lstEmployee.RemoveItem n

Next n Answer:

Page 9: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

9Combo box control

A Combo box control combines the features of a Text box control and a List box control– Users can enter information in the text box portion or

select an item from the list box portion of the control.

Combo boxes and List boxes have essentially the same – properties – Events– and methods

Page 10: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

10Combo box control

Common properties:– List Array of items in the list box (Note: index=0 through n)– Sorted True or False (displaying items in alphabetical order)– Style 0 (Dropdown Combo). Includes a drop-down list

and a text box. 1 (Simple Combo). Includes a text box and a list,

which doesn't drop down. – Text Returns item currently highlighted

Examples: cboEmployee.List(0) = “John” cboEmployee.Sorted = True cboEmployee.Style = 1 picOutput.Print cboEmployee.Text

Page 11: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

11Combo box control

Common Methods:– AddItem item Adds the item referred to as item to the list– RemoveItem n Removes the item of index n from the combo box– Clear Deletes all items in the combo box– ListCount Returns the # of items in the combo box– ListIndex Returns the index of the item currently

highlighted in the combo box. – NewIndex Returns the index of the most recently added

item

Examples: – cboEmployee.AddItem “John”– cboEmployee.RemoveItem 2– cboEmployee.Clear– NumberOfEmployees = cboEmployee.ListCount– picSelected.Print cboEmployee.NewIndex

Page 12: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

12Combo box: Exercise

Suppose that the combo box cboEmployee appears as shown and that the Sorted property is set to True. Give a statement or statements that carry out the stated task.

Display the item “Barbara Lewis” in picOutput

Answer:

Display the item “John Whashington” in a picOutput

Answer:

Delete the item “Nathalie Melrose”

Answer:

Page 13: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

13Combo box: Exercise

Suppose that the combo box cboEmployee appears as shown and that the Sorted property is set to True. Give a statement or statements that carry out the stated task.

Add the item “Liza Frulla”. Where will it be inserted?

Answer:

Delete every item beginning with the letter “M”. Use a Do While loop and an If test.

Answer:

Suppose that you should use For..Next instead of Do While, give the correct statements.

Answer

Page 14: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

14DriveList box control

A DriveListBox control:– Displays a list of all the valid drives on the computer– Enables a user to select a valid disk drive at run time.

No code is needed to load a DriveList box

Most used property:– Drive Contains the name of the currently selected drive

Example: Drive = drvExample.Drive

Most used Event– Change Happens when the user or program changes the drive selection

Example: Private Sub drvExample_Change() picOutput.Print “The New drive is ”;

drvExample.DriveEnd Sub

Page 15: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

15DirList box control

A DirList Box control:– Displays an ordered, hierarchical list of directories and subdirectories

Most used property:– Path Contains the current directory path

Example: Path = dirExample.Path

Most used Event– Change Happens the directory selection is changed

Example: Private Sub dirExample_Change() picOutput.Print dirExample.PathEnd Sub

Page 16: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

16FileList box control

A FileList Box control:– Locates and lists files in the directory specified by the Path property at run time

Most used properties:– FileName Contains the current selected file name– ListCount Number of files listed– List Array of file names in the list box– Path Contains the current directory path– Pattern Contains a string that determine which files will be

displayed (Example: *.doc only displays files with the .doc extension)

Most used Events– DblClick Happens whenever a file name is double-clicked.– PathChange Happens whenever the path changes

Example: Private Sub filExample_PathChange () picOutput.Print filExample.PathEnd Sub

Page 17: List box & Combo box controls School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 14, Monday 4/21/2003)

17Synchronizing Drive, Directory and File list boxes

Drive, Directory, and File list boxes are almost always used together to obtain a file name.

– As such, it is important that their operation be synchronized to insure the displayed information is always consistent.

When the directory selection is changed (directory box Change event), you should update the displayed file names.

Private Sub dirExample_Change() filExample.Path = dirExample.PathEnd Sub

When the drive selection is changed (drive box Change event), you should update the directory path

Private Sub drvExample_Change() dirExample.Path = drvExample.DriveEnd Sub