MA3696 Lecture 8

36
ListBoxes Selecting items LECTURE 8

Transcript of MA3696 Lecture 8

Page 1: MA3696 Lecture 8

ListBoxes

Selecting items LECTURE 8

Page 2: MA3696 Lecture 8

ListBoxes and CheckBoxes

Selecting items from a list

Also select corresponding information

Show selected items

ListBoxes with multiple columns

SUMMARY

Page 3: MA3696 Lecture 8

Displays a list of items

Each item has a position in the ListBox

Count from the 0 position

LISTBOX PROPERTIES

Select ONE item Select MULTI items

0 1 2 3 4 5 6 7

Page 4: MA3696 Lecture 8

.RowSource

.List

.AddItem ADD ITEMS TO A LISTBOX

Page 5: MA3696 Lecture 8

If the named range is a column

ListBox.RowSource = “RangeName”

Where RangeName is the name of the range in Excel

LISTBOX PROPERTIES: ADD A LIST

Items from a Named Range

List_CarBrands.RowSource = “CarBrands”

Page 6: MA3696 Lecture 8

If the named range is a row

ListBox.List = Application.Transpose(Range(“RangeName”))

Where RangeName is the name of the range in Excel

LISTBOX PROPERTIES: ADD A LIST

Items from a Named Range

List_CarBrands.List =Application.Transpose(Range(“CarBrands”))

Page 7: MA3696 Lecture 8

ListBox.List = NameOfArray

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.

LISTBOX PROPERTIES: ADD A LIST

List_CarBrands.List = carBrands

Items from an array

Page 8: MA3696 Lecture 8

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 “ ”

You can use a With statement just like we did for ComboBoxes.

LISTBOX PROPERTIES: ADD ITEMS

Items one at a time

List_CarBrands.AddItem “BMW”

With List_CarBrands .AddItem “BMW” .AddItem “Fiat” End With

You can use ranges instead of text

Page 9: MA3696 Lecture 8

.Clear CLEAR THE LIST

Page 10: MA3696 Lecture 8

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

LISTBOX PROPERTIES: CLEAR LIST

Clear entire list

Page 11: MA3696 Lecture 8

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

L i s tBox propert ies for adding and c lear ing i tems f rom a L i s tBox

QUICK SUMMARY

Page 12: MA3696 Lecture 8

Download Lecture 8 Student Example.xlsm

Open Userform1

Use one of the methods just shown to add the car brands to List_CarBrands.

EXERCISE 1. ADD ITEMS TO A LIST

Page 13: MA3696 Lecture 8

.ListCount

.Value

.ListIndex

.List( i)

.Selected(i)

GET INFO ABOUT WHAT IS IN THE LISTBOX

(AND USER SELECTIONS)

Page 14: MA3696 Lecture 8

ListBox.ListCount

Returns the number of items shown in the ListBox

What is the value of numCars?

How many times will this loop run?

LISTBOX PROPERTIES: COUNT ITEMS

Count number of list items

Dim numCars as Integer numCars = List_CarBrands.ListCount

For i = 1 to List_CarBrands.ListCount … Next i

Page 15: MA3696 Lecture 8

ListBox.Value

Returns the item that is selected in the list

ONLY works for Single Select ListBoxes NOT Multi Select

What will be shown in the message box?

What will be outputted to the above named range?

LISTBOX PROPERTIES: WHAT’S SELECTED?

What item was selected?

MsgBox (List_CarBrands.Value)

Range(“SelectedCar”).Value = List_CarBrands.Value

Page 16: MA3696 Lecture 8

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.

What will be shown in the message box?

LISTBOX PROPERTIES: WHAT’S SELECTED?

MsgBox (List_CarBrands.ListIndex)

The position of selected item

Page 17: MA3696 Lecture 8

ListBox.List(i)

Returns the ith 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) = …

What will be shown in the message box?

LISTBOX PROPERTIES: GET ITEMS FROM LIST

What is the ith list item?

MsgBox (List_CarBrands.List(2))

Page 18: MA3696 Lecture 8

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:

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 If-statement or Select Case (you’ll need a For -loop too)

LISTBOX PROPERTIES: WHAT’S SELECTED?

i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7

What item(s) were selected?

Page 19: MA3696 Lecture 8

Open UserForm1

When the command button is clicked

Loop through each item in the ListBox (use .ListCount)

Inside the loop, test if the ith item has been selected

If it has been selected, then show the selection in a message box.

EXERCISE 2. TEST WHAT IS SELECTED

i=1 i=4

i=5 i=7

Page 20: MA3696 Lecture 8

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 ith list item

ListBox.Selected(i) TRUE if the ith item is selected FALSE if the ith item is not selected

L i s tBox propert ies for gett ing info about l i s t i tems and user se lect ions

QUICK SUMMARY

Page 21: MA3696 Lecture 8

.Selected(i) SELECT LIST ITEMS

WITHIN YOUR CODE

Page 22: MA3696 Lecture 8

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

LISTBOX PROPERTIES: MAKE SELECTIONS

i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7

Select or unselect list items

Page 23: MA3696 Lecture 8

Write code to select Mercedes from the listbox:

Mercedes is 4th, but counting from 0 it is 3rd.

EXAMPLE. SELECT ITEMS IN A LIST

List_CarBrands.Selected(3) = True

Page 24: MA3696 Lecture 8

Write code to select Fiat, Mercedes and Porche:

EXAMPLE. SELECT ITEMS IN A LIST

List_CarBrands.Selected(1) = True

List_CarBrands.Selected(3) = True

List_CarBrands.Selected(5) = True

Make sure List_CarBrands is MultiSelect

Page 25: MA3696 Lecture 8

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

EXERCISE 3. SELECT ITEMS IN A LIST

Make sure List_CarBrands is MultiSelect

Page 26: MA3696 Lecture 8

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.

EXERCISE 4. UNSELECT ITEMS IN A LIST

Page 27: MA3696 Lecture 8

What’s the difference between…

ListBox.Value,

ListBox.List(),

ListBox.ListIndex and

ListBox.Selected()?

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 ith item. That item may or may not have been selected.

LISTBOX PROPERTIES

These 2 only work for single select ListBoxes

Page 28: MA3696 Lecture 8

Multi-column LISTBOXES

Page 29: MA3696 Lecture 8

Change the ColumnCount property to the number of columns you want

Use .RowSource to add a named range

MULTI-COLUMN LISTBOXES

Page 30: MA3696 Lecture 8

MULTI-COLUMN LISTBOXES

The number of columns

Page 31: MA3696 Lecture 8

MULTI-COLUMN LISTBOXES

Do you want column headings?

True = Yes, False = No

Page 32: MA3696 Lecture 8

COLUMNHEADINGS = TRUE

Takes the row ABOVE your range as the headings

Page 33: MA3696 Lecture 8

MULTI-COLUMN LISTBOXES

Adjust width of columns Separate with ;

Page 34: MA3696 Lecture 8

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.

EXERCISE 5. MULTI-COLUMN LISTBOX

Page 35: MA3696 Lecture 8

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.

LEARNING OUTCOMES

Page 36: MA3696 Lecture 8

THE END