SlideShare une entreprise Scribd logo
1  sur  36
Télécharger pour lire hors ligne
LECTURE 8

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

 Show selected items

ListBoxes with multiple columns
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
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”
 Where RangeName is the name of the range in Excel
List_CarBrands.RowSource = “CarBrands”
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”))
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.
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
CLEAR THE LIST

.Clear
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
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
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.
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

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?
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?
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?
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?
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)
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
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
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 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
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
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
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
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.
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.
LISTBOXES

Multi-column
MULTI-COLUMN LISTBOXES

Change the ColumnCount property to the number
of columns you want
Use .RowSource to add a named range
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 prices.
 Make the ListBox multi-column.
 Name a range containing all prices as wells as row labels.
 Show this named range in your ListBox.
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.
THE END

Contenu connexe

Similaire à MA3696 Lecture 8

In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfEvanpZjSandersony
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
Vb6 ch.7-3 cci
Vb6 ch.7-3 cciVb6 ch.7-3 cci
Vb6 ch.7-3 cciFahim Khan
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxSHIVA101531
 
This application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdfThis application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdfambikacomputer4301
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdffashiongallery1
 
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docxAD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docxAmuthachenthiruK
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eGina Bullock
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eGina Bullock
 
Question 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdfQuestion 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdfclimatecontrolsv
 
Question 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdfQuestion 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdfclimatecontrolsv
 
How to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepHow to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepSelect Distinct Limited
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfalivaisi1
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsAbdullah Al-hazmy
 

Similaire à MA3696 Lecture 8 (20)

Array list(1)
Array list(1)Array list(1)
Array list(1)
 
In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
 
Lists
ListsLists
Lists
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
Vb6 ch.7-3 cci
Vb6 ch.7-3 cciVb6 ch.7-3 cci
Vb6 ch.7-3 cci
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
 
This application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdfThis application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdf
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docxAD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5e
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5e
 
Question 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdfQuestion 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdf
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Question 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdfQuestion 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdf
 
How to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepHow to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by step
 
C# Collection classes
C# Collection classesC# Collection classes
C# Collection classes
 
Ms excel
Ms excelMs excel
Ms excel
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdf
 
Csharp_List
Csharp_ListCsharp_List
Csharp_List
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
 

Plus de Brunel University (9)

MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
MA3696 Lecture 7
MA3696 Lecture 7MA3696 Lecture 7
MA3696 Lecture 7
 
MA3696 Lecture 6
MA3696 Lecture 6MA3696 Lecture 6
MA3696 Lecture 6
 
MA3696 Lecture 5
MA3696 Lecture 5MA3696 Lecture 5
MA3696 Lecture 5
 
Ma3696 lecture 4
Ma3696 lecture 4Ma3696 lecture 4
Ma3696 lecture 4
 
Ma3696 Lecture 3
Ma3696 Lecture 3Ma3696 Lecture 3
Ma3696 Lecture 3
 
Ma3696 Lecture 2
Ma3696 Lecture 2Ma3696 Lecture 2
Ma3696 Lecture 2
 
Ma3696 Lecture 0
Ma3696 Lecture 0Ma3696 Lecture 0
Ma3696 Lecture 0
 
Ma3696 Lecture 1
Ma3696 Lecture 1Ma3696 Lecture 1
Ma3696 Lecture 1
 

Dernier

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Dernier (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

MA3696 Lecture 8

  • 2. SUMMARY  ListBoxes and CheckBoxes  Selecting items from a list  Also select corresponding information  Show selected items ListBoxes with multiple columns
  • 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. ADD ITEMS TO A LISTBOX .RowSource .List .AddItem
  • 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. 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. 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. 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
  • 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. 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. 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. GET INFO ABOUT WHAT IS IN THE LISTBOX (AND USER SELECTIONS) .ListCount .Value .ListIndex .List(i) .Selected(i)
  • 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. 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. 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. 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. 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. 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. 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. SELECT LIST ITEMS WITHIN YOUR CODE .Selected(i)
  • 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. 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. 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. 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. 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. 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.
  • 29. MULTI-COLUMN LISTBOXES Change the ColumnCount property to the number of columns you want Use .RowSource to add a named range
  • 31. MULTI-COLUMN LISTBOXES Do you want column headings? True = Yes, False = No
  • 32. COLUMNHEADINGS = TRUE Takes the row ABOVE your range as the headings
  • 33. MULTI-COLUMN LISTBOXES Adjust width of columns Separate with ;
  • 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. 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.