Difference between revisions of "Lua:Class:ComboBox"
Jump to navigation
Jump to search
(Major overhaul of the post.) |
|||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | + | {{Class|'''class''' ComboBox ''':''' WinControl}} | |
| − | + | The ComboBox class represents a combo box control. | |
| − | |||
| − | + | A combo box allows the user to select one entry from a list of items. It can be created with [[Lua:createComboBox|createComboBox]]. | |
| − | |||
| − | |||
| − | |||
| − | === | + | ===Inheritance=== |
| − | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | |
| − | + | !align="left"|Class | |
| − | + | !align="left"|Inherits From | |
| − | + | !style="width: 80%;background-color:white;" align="left"|Description | |
| − | + | |- | |
| − | + | |ComboBox | |
| + | |WinControl | ||
| + | |Represents a combo box control. | ||
| + | |- | ||
| + | |WinControl | ||
| + | |Control | ||
| + | |Base class for controls that can receive focus and contain child controls. | ||
| + | |- | ||
| + | |Control | ||
| + | |Component | ||
| + | |Base class for visible GUI controls. | ||
| + | |- | ||
| + | |Component | ||
| + | |Object | ||
| + | |Base class for components. | ||
| + | |- | ||
| + | |Object | ||
| + | |None | ||
| + | |Base class for Lua-exposed objects. | ||
| + | |} | ||
| − | === | + | == Creation == |
| − | + | ||
| − | + | {{CodeBox|'''function''' createComboBox(''owner'') ''':''' ComboBox}} | |
| − | + | ||
| − | + | Creates a ComboBox object that belongs to the given owner. | |
| − | + | ||
| − | + | The owner can be any object inherited from [[Lua:Class:WinControl|WinControl]]. | |
| + | |||
| + | ===Function Parameters=== | ||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |owner | ||
| + | |[[Lua:Class:WinControl|WinControl]] | ||
| + | |The owner of the combo box. | ||
| + | |} | ||
| + | |||
| + | ===Returns=== | ||
| + | [[Lua:Class:ComboBox|ComboBox]] — The created combo box object. | ||
| + | |||
| + | == Properties == | ||
| + | |||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Property | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |Items | ||
| + | |[[Lua:Class:Strings|Strings]] | ||
| + | |A Strings-derived object containing all items in the list. | ||
| + | |- | ||
| + | |ItemIndex | ||
| + | |Integer | ||
| + | |The selected item index. -1 means no item is selected. | ||
| + | |- | ||
| + | |Canvas | ||
| + | |[[Lua:Class:Canvas|Canvas]] | ||
| + | |The canvas object used to render on the combo box. | ||
| + | |} | ||
| + | |||
| + | == Methods == | ||
| + | |||
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Method | ||
| + | !align="left"|Return Type | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |clear() | ||
| + | |void | ||
| + | |Removes all items from the combo box. | ||
| + | |- | ||
| + | |getItems() | ||
| + | |[[Lua:Class:Strings|Strings]] | ||
| + | |Returns the Strings object containing the combo box items. | ||
| + | |- | ||
| + | |setItems(items) | ||
| + | |void | ||
| + | |Sets the combo box items. | ||
| + | |- | ||
| + | |getItemIndex() | ||
| + | |Integer | ||
| + | |Returns the selected item index. | ||
| + | |- | ||
| + | |setItemIndex(index) | ||
| + | |void | ||
| + | |Sets the selected item index. | ||
| + | |- | ||
| + | |getCanvas() | ||
| + | |[[Lua:Class:Canvas|Canvas]] | ||
| + | |Returns the canvas object used to render on the combo box. | ||
| + | |- | ||
| + | |getExtraWidth() | ||
| + | |Integer | ||
| + | |Returns the number of pixels not part of the text area of the combo box, such as borders and the drop-down button. | ||
| + | |} | ||
| + | |||
| + | == Examples == | ||
| + | |||
| + | ===Create a ComboBox=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | comboBox.Left = 10 | ||
| + | comboBox.Top = 10 | ||
| + | comboBox.Width = 180 | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Add items to a ComboBox=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,4,5,6"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | comboBox.Items.add("First item") | ||
| + | comboBox.Items.add("Second item") | ||
| + | comboBox.Items.add("Third item") | ||
| + | |||
| + | comboBox.ItemIndex = 0 | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Read the selected item index=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,8"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | comboBox.Items.add("Low") | ||
| + | comboBox.Items.add("Medium") | ||
| + | comboBox.Items.add("High") | ||
| + | |||
| + | local index = comboBox.ItemIndex | ||
| + | |||
| + | print("Selected index: " .. tostring(index)) | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Set the selected item index=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,7"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | comboBox.Items.add("Small") | ||
| + | comboBox.Items.add("Normal") | ||
| + | comboBox.Items.add("Large") | ||
| + | comboBox.ItemIndex = 1 | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Use getItems=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,4"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | local items = comboBox.getItems() | ||
| + | |||
| + | items.add("Red") | ||
| + | items.add("Green") | ||
| + | items.add("Blue") | ||
| + | |||
| + | comboBox.ItemIndex = 0 | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Use getItemIndex and setItemIndex=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,8,10"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | comboBox.Items.add("Option A") | ||
| + | comboBox.Items.add("Option B") | ||
| + | comboBox.Items.add("Option C") | ||
| + | |||
| + | comboBox.setItemIndex(2) | ||
| + | |||
| + | print("Selected index: " .. tostring(comboBox.getItemIndex())) | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Clear all items=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,8"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | comboBox.Items.add("Temporary A") | ||
| + | comboBox.Items.add("Temporary B") | ||
| + | comboBox.Items.add("Temporary C") | ||
| + | |||
| + | comboBox.clear() | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Get the combo box canvas=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,4"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | local canvas = comboBox.getCanvas() | ||
| + | |||
| + | print(canvas.ClassName) | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===Get the extra width=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,4"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | local extraWidth = comboBox.getExtraWidth() | ||
| + | |||
| + | print("Extra width: " .. tostring(extraWidth)) | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ===React to a changed selection=== | ||
| + | <syntaxhighlight lang="lua" line highlight="2,9"> | ||
| + | local form = createForm(false) | ||
| + | local comboBox = createComboBox(form) | ||
| + | |||
| + | comboBox.Items.add("Beginner") | ||
| + | comboBox.Items.add("Advanced") | ||
| + | comboBox.Items.add("Expert") | ||
| + | |||
| + | comboBox.OnChange = function(sender) | ||
| + | print("Selected index: " .. tostring(sender.ItemIndex)) | ||
| + | end | ||
| + | |||
| + | comboBox.ItemIndex = 0 | ||
| + | |||
| + | form.show() | ||
| + | </syntaxhighlight> | ||
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| + | |||
| + | {{Forms}} | ||
Latest revision as of 19:45, 26 June 2026
The ComboBox class represents a combo box control.
A combo box allows the user to select one entry from a list of items. It can be created with createComboBox.
Inheritance[edit]
| Class | Inherits From | Description |
|---|---|---|
| ComboBox | WinControl | Represents a combo box control. |
| WinControl | Control | Base class for controls that can receive focus and contain child controls. |
| Control | Component | Base class for visible GUI controls. |
| Component | Object | Base class for components. |
| Object | None | Base class for Lua-exposed objects. |
Creation[edit]
Creates a ComboBox object that belongs to the given owner.
The owner can be any object inherited from WinControl.
Function Parameters[edit]
| Parameter | Type | Description |
|---|---|---|
| owner | WinControl | The owner of the combo box. |
Returns[edit]
ComboBox — The created combo box object.
Properties[edit]
| Property | Type | Description |
|---|---|---|
| Items | Strings | A Strings-derived object containing all items in the list. |
| ItemIndex | Integer | The selected item index. -1 means no item is selected. |
| Canvas | Canvas | The canvas object used to render on the combo box. |
Methods[edit]
| Method | Return Type | Description |
|---|---|---|
| clear() | void | Removes all items from the combo box. |
| getItems() | Strings | Returns the Strings object containing the combo box items. |
| setItems(items) | void | Sets the combo box items. |
| getItemIndex() | Integer | Returns the selected item index. |
| setItemIndex(index) | void | Sets the selected item index. |
| getCanvas() | Canvas | Returns the canvas object used to render on the combo box. |
| getExtraWidth() | Integer | Returns the number of pixels not part of the text area of the combo box, such as borders and the drop-down button. |
Examples[edit]
Create a ComboBox[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 comboBox.Left = 10
5 comboBox.Top = 10
6 comboBox.Width = 180
7
8 form.show()
Add items to a ComboBox[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 comboBox.Items.add("First item")
5 comboBox.Items.add("Second item")
6 comboBox.Items.add("Third item")
7
8 comboBox.ItemIndex = 0
9
10 form.show()
Read the selected item index[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 comboBox.Items.add("Low")
5 comboBox.Items.add("Medium")
6 comboBox.Items.add("High")
7
8 local index = comboBox.ItemIndex
9
10 print("Selected index: " .. tostring(index))
11
12 form.show()
Set the selected item index[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 comboBox.Items.add("Small")
5 comboBox.Items.add("Normal")
6 comboBox.Items.add("Large")
7 comboBox.ItemIndex = 1
8
9 form.show()
Use getItems[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 local items = comboBox.getItems()
5
6 items.add("Red")
7 items.add("Green")
8 items.add("Blue")
9
10 comboBox.ItemIndex = 0
11
12 form.show()
Use getItemIndex and setItemIndex[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 comboBox.Items.add("Option A")
5 comboBox.Items.add("Option B")
6 comboBox.Items.add("Option C")
7
8 comboBox.setItemIndex(2)
9
10 print("Selected index: " .. tostring(comboBox.getItemIndex()))
11
12 form.show()
Clear all items[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 comboBox.Items.add("Temporary A")
5 comboBox.Items.add("Temporary B")
6 comboBox.Items.add("Temporary C")
7
8 comboBox.clear()
9
10 form.show()
Get the combo box canvas[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 local canvas = comboBox.getCanvas()
5
6 print(canvas.ClassName)
7
8 form.show()
Get the extra width[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 local extraWidth = comboBox.getExtraWidth()
5
6 print("Extra width: " .. tostring(extraWidth))
7
8 form.show()
React to a changed selection[edit]
1 local form = createForm(false)
2 local comboBox = createComboBox(form)
3
4 comboBox.Items.add("Beginner")
5 comboBox.Items.add("Advanced")
6 comboBox.Items.add("Expert")
7
8 comboBox.OnChange = function(sender)
9 print("Selected index: " .. tostring(sender.ItemIndex))
10 end
11
12 comboBox.ItemIndex = 0
13
14 form.show()