Your SlideShare is downloading. ×
0
LECTURE 8

ListBoxes
Selecting items
SUMMARY
 ListBoxes and CheckBoxes
 Selecting items from a list
 Also select corresponding information

 Show selected ...
LISTBOX PROPERTIES
Displays a list of items
0
1
2
3
4
5
6
7

Select ONE item

Select MULTI items

Each item has a positi...
ADD ITEMS TO A LISTBOX

.RowSource
.List
.AddItem
LISTBOX PROPERTIES: ADD A LIST
Items from a Named Range

If the named range is a column
 ListBox.RowSource = “RangeName”...
LISTBOX PROPERTIES: ADD A LIST
Items from a Named Range

If the named range is a row
 ListBox.List = Application.Transpo...
LISTBOX PROPERTIES: ADD A LIST
Items from an array

ListBox.List = NameOfArray

List_CarBrands.List = carBrands

 Where ...
LISTBOX PROPERTIES: ADD ITEMS
Items one at a time

ListBox.AddItem ListItem
 Where ListItem is the item you want to add ...
CLEAR THE LIST

.Clear
LISTBOX PROPERTIES: CLEAR LIST
Clear entire list

ListBox.Clear
If you are showing what the user has selected in a
secon...
ListBox.RowSource
Add a named range (column)

ListBox.List
Add a named range (row)
Add items from an array

ListBox....
EXERCISE 1. ADD ITEMS TO A LIST
Download Lecture 8 Student Example.xlsm
Open Userform1
Use one of the methods just show...
GET INFO ABOUT WHAT IS
IN THE LISTBOX
(AND USER SELECTIONS)

.ListCount
.Value
.ListIndex
.List(i)
.Selected(i)
LISTBOX PROPERTIES: COUNT ITEMS
Count number of list items

ListBox.ListCount
 Returns the number of items shown in the ...
LISTBOX PROPERTIES: WHAT’S SELECTED?
What item was selected?

ListBox.Value
 Returns the item that is selected in the li...
LISTBOX PROPERTIES: WHAT’S SELECTED?
The position of selected item

ListBox.ListIndex
 Returns the position of the item ...
LISTBOX PROPERTIES: GET ITEMS FROM LIST
What is the ith list item?

ListBox.List(i)
 Returns the i th item from the list...
LISTBOX PROPERTIES: WHAT’S SELECTED?
What item(s) were selected?

ListBox.Selected(i) is True or False
 = True if item i...
EXERCISE 2. TEST WHAT IS SELECTED
Open UserForm1
When the command button is clicked
 Loop through each item in the List...
ListBox.ListCount
Count the number of list items

ListBox.Value
Return the selected item

ListBox.ListIndex
Return t...
SELECT LIST ITEMS
WITHIN YOUR CODE

.Selected(i)
LISTBOX PROPERTIES: MAKE SELECTIONS
Select or unselect list items

ListBox.Selected(i) is True or False
 = True if item ...
EXAMPLE. SELECT ITEMS IN A LIST
 Write code to select Mercedes from the listbox:
 Mercedes is 4 th , but counting from 0...
EXAMPLE. SELECT ITEMS IN A LIST
Write code to select Fiat, Mercedes and Porche:

List_CarBrands.Selected(1) = True
List_C...
EXERCISE 3. SELECT ITEMS IN A LIST
Open Userform2. Add car brands to the ListBox.
A checkbox can have a value of TRUE or...
EXERCISE 4. UNSELECT ITEMS IN A LIST
Open Userform2
Amend your code from Exercise 2
 If the checkbox is de-selected (i....
LISTBOX PROPERTIES
What’s the difference between…
 ListBox.Value,
 ListBox.List(),
 ListBox.ListIndex and
 ListBox.Se...
LISTBOXES

Multi-column
MULTI-COLUMN LISTBOXES

Change the ColumnCount property to the number
of columns you want
Use .RowSource to add a named ...
MULTI-COLUMN LISTBOXES

The number of columns
MULTI-COLUMN LISTBOXES

Do you want column
headings?
True = Yes, False = No
COLUMNHEADINGS = TRUE

Takes the row ABOVE your range as the headings
MULTI-COLUMN LISTBOXES

Adjust width of columns
Separate with ;
EXERCISE 5. MULTI-COLUMN LISTBOX
Insert a new userform.
Insert a large ListBox big enough to show all car
brands and pri...
LEARNING OUTCOMES
You are ready to move on when…
 LO32: You can add items one by one or as a list to a
ListBox.
 LO33: ...
THE END
Upcoming SlideShare
Loading in...5
×

MA3696 Lecture 8

95

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
95
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "MA3696 Lecture 8"

  1. 1. LECTURE 8 ListBoxes Selecting items
  2. 2. SUMMARY  ListBoxes and CheckBoxes  Selecting items from a list  Also select corresponding information  Show selected items ListBoxes with multiple columns
  3. 3. LISTBOX PROPERTIES Displays a list of items 0 1 2 3 4 5 6 7 Select ONE item Select MULTI items Each item has a position in the ListBox  Count from the 0 position
  4. 4. ADD ITEMS TO A LISTBOX .RowSource .List .AddItem
  5. 5. LISTBOX PROPERTIES: ADD A LIST Items from a Named Range If the named range is a column  ListBox.RowSource = “RangeName”  Where RangeName is the name of the range in Excel List_CarBrands.RowSource = “CarBrands”
  6. 6. LISTBOX PROPERTIES: ADD A LIST Items from a Named Range If the named range is a row  ListBox.List = Application.Transpose(Range(“RangeName”))  Where RangeName is the name of the range in Excel List_CarBrands.List =Application.Transpose(Range(“CarBrands”))
  7. 7. LISTBOX PROPERTIES: ADD A LIST Items from an array ListBox.List = NameOfArray List_CarBrands.List = carBrands  Where carBrands() is an array containing the names of the brands of cars  carBrands() has been declared and assigned the correct size and dimension. I have assigned the names from excel to the corresponding element of the array using a For loop.
  8. 8. LISTBOX PROPERTIES: ADD ITEMS Items one at a time ListBox.AddItem ListItem  Where ListItem is the item you want to add to the list  There is NO =  If ListItem is text it needs to be in “ ” List_CarBrands.AddItem “BMW” You can use a With statement just like we did for ComboBoxes. With List_CarBrands .AddItem “BMW” .AddItem “Fiat” End With You can use ranges instead of text
  9. 9. CLEAR THE LIST .Clear
  10. 10. LISTBOX PROPERTIES: CLEAR LIST Clear entire list ListBox.Clear If you are showing what the user has selected in a second ListBox, you may need to clear it if they change their selection
  11. 11. ListBox.RowSource Add a named range (column) ListBox.List Add a named range (row) Add items from an array ListBox.AddItem Add items one by one ListBox.Clear Clear all items in the ListBox QUICK SUMMARY L i s t B ox p ro p e r t i e s fo r adding and c l e a r i n g i te m s f ro m a L i s t B ox
  12. 12. EXERCISE 1. ADD ITEMS TO A LIST Download Lecture 8 Student Example.xlsm Open Userform1 Use one of the methods just shown to add the car brands to List_CarBrands.
  13. 13. GET INFO ABOUT WHAT IS IN THE LISTBOX (AND USER SELECTIONS) .ListCount .Value .ListIndex .List(i) .Selected(i)
  14. 14. LISTBOX PROPERTIES: COUNT ITEMS Count number of list items ListBox.ListCount  Returns the number of items shown in the ListBox Dim numCars as Integer numCars = List_CarBrands.ListCount  What is the value of numCars? For i = 1 to List_CarBrands.ListCount … Next i  How many times will this loop run?
  15. 15. LISTBOX PROPERTIES: WHAT’S SELECTED? What item was selected? ListBox.Value  Returns the item that is selected in the list  ONLY works for Single Select ListBoxes NOT Multi Select MsgBox (List_CarBrands.Value)  What will be shown in the message box? Range(“SelectedCar”).Value = List_CarBrands.Value  What will be outputted to the above named range?
  16. 16. LISTBOX PROPERTIES: WHAT’S SELECTED? The position of selected item ListBox.ListIndex  Returns the position of the item that is selected in the list  Will only return a number  ONLY works for Single Select ListBoxes NOT Multi Select  Counts from 0. MsgBox (List_CarBrands.ListIndex)  What will be shown in the message box?
  17. 17. LISTBOX PROPERTIES: GET ITEMS FROM LIST What is the ith list item? ListBox.List(i)  Returns the i th item from the list  Works for Single Select and Multi Select  Counts from 0  We cannot use it to add items to a list  E.g., ListBox.List(i) = … MsgBox (List_CarBrands.List(2))  What will be shown in the message box?
  18. 18. LISTBOX PROPERTIES: WHAT’S SELECTED? What item(s) were selected? ListBox.Selected(i) is True or False  = True if item is selected  = False if item is not selected  Where i is a number or variable  i represents a position in the ListBox  Counts from 0 i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 Use .Selected(i) to:  Test whether an item in the list has been selected from Multi-select ListBox (use .ListIndex or .Value for single select)  Test whether ListBox.Selected(i) is TRUE or FALSE using an Ifstatement or Select Case (you’ll need a For-loop too)
  19. 19. EXERCISE 2. TEST WHAT IS SELECTED Open UserForm1 When the command button is clicked  Loop through each item in the ListBox (use .ListCount)  Inside the loop, test if the i th item has been selected  If it has been selected, then show the selection in a message box. i=1 i=4 i=5 i=7
  20. 20. ListBox.ListCount Count the number of list items ListBox.Value Return the selected item ListBox.ListIndex Return the position of selected item ListBox.List(i) Return the i th list item ListBox.Selected(i) TRUE if the i th item is selected FALSE if the i th item is not selected QUICK SUMMARY L i s t B ox p ro p e r t i e s fo r g ett i n g i nfo a b o u t l i s t i te m s and user selections
  21. 21. SELECT LIST ITEMS WITHIN YOUR CODE .Selected(i)
  22. 22. LISTBOX PROPERTIES: MAKE SELECTIONS Select or unselect list items ListBox.Selected(i) is True or False  = True if item is selected  = False if item is not selected  Where i is a number or variable  i represents a position in the ListBox  Counts from 0 Use .Selected(i) to:  Select an item in the list from within the code  Deselect an item in the list from within the code i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7
  23. 23. EXAMPLE. SELECT ITEMS IN A LIST  Write code to select Mercedes from the listbox:  Mercedes is 4 th , but counting from 0 it is 3 rd . List_CarBrands.Selected(3) = True
  24. 24. EXAMPLE. SELECT ITEMS IN A LIST Write code to select Fiat, Mercedes and Porche: List_CarBrands.Selected(1) = True List_CarBrands.Selected(3) = True List_CarBrands.Selected(5) = True Make sure List_CarBrands is MultiSelect
  25. 25. EXERCISE 3. SELECT ITEMS IN A LIST Open Userform2. Add car brands to the ListBox. A checkbox can have a value of TRUE or FALSE  checkbox1.value = True (means it’s checked)  checkbox1.value = False (means it’s not checked) In the procedure for clicking the check box:  Write code to check if the value of the checkbox is true  If it is true (so it’s checked) then select all items in the ListBox Make sure List_CarBrands is MultiSelect
  26. 26. EXERCISE 4. UNSELECT ITEMS IN A LIST Open Userform2 Amend your code from Exercise 2  If the checkbox is de-selected (i.e., the value is not true) then unselect every item in the list.
  27. 27. LISTBOX PROPERTIES What’s the difference between…  ListBox.Value,  ListBox.List(),  ListBox.ListIndex and  ListBox.Selected()? These 2 only work for single select ListBoxes ListBox.Selected() is equal to TRUE or FALSE only. ListBox.ListIndex is equal to the numbered position of the selected item. ListBox.Value is equal to the value of the selected item. ListBox.List() is equal to the value of the i th item. That item may or may not have been selected.
  28. 28. LISTBOXES Multi-column
  29. 29. MULTI-COLUMN LISTBOXES Change the ColumnCount property to the number of columns you want Use .RowSource to add a named range
  30. 30. MULTI-COLUMN LISTBOXES The number of columns
  31. 31. MULTI-COLUMN LISTBOXES Do you want column headings? True = Yes, False = No
  32. 32. COLUMNHEADINGS = TRUE Takes the row ABOVE your range as the headings
  33. 33. MULTI-COLUMN LISTBOXES Adjust width of columns Separate with ;
  34. 34. EXERCISE 5. MULTI-COLUMN LISTBOX Insert a new userform. Insert a large ListBox big enough to show all car brands and prices.  Make the ListBox multi-column.  Name a range containing all prices as wells as row labels.  Show this named range in your ListBox.
  35. 35. LEARNING OUTCOMES You are ready to move on when…  LO32: You can add items one by one or as a list to a ListBox.  LO33: You can determine which ListBox property should be used to get information about the ListBox and user selections.  LO34: You can create a multi-column ListBox using a named range in Excel.
  36. 36. THE END
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×