-
Notifications
You must be signed in to change notification settings - Fork 7
HowTo : ComboBox widget
This wxRuby guide demonstrates how to use the Wx::ComboBox widget with it’s various styles, features and functions. A complete list of options will be included here together with code examples for your convenience.
The syntax for creating a ComboBox widget in wxRuby is as follows:
cb = Wx::ComboBox.new(parent, id, value, pos, size, choices, style, validator, name)
Parameter descriptions:
-
parent : Wx::Window
the parent window (widget) such as a Wx::Panel. -
id : Integer
Button ID.Wx::ID_ANY
indicates a default value. -
value : String
Optional text to display in the widget as the initial selection. Default is empty string. -
pos : Array(Integer,Integer) or Wx::Point
Optional coordinates for the position of the topleft corner of the widget. Default isWx::DEFAULT_POSITION
. -
size : Array(Integer,Integer) or Wx::Size
Optional dimensions of the widget. Default isWx::DEFAULT_SIZE
. -
choices : Array<String>
A list of strings which will be used as options for the Combobox. Default isnil
(no options). -
style : Integer
Optional styling mask for the button (such as alignment). Default is 0. -
validator : Wx::Validator
Optional Window validator. Default is nil. -
name : String
Optional window name. Default is Wx::COMBO_BOX_NAME_STR.
Note: As with all windows, keyword constructor alternatives for all arguments but the 'parent' argument are available. See here for more information.
Available styles for the ComboBox widget:
Button Style | Description |
---|---|
Wx::CB_SIMPLE | Creates a combobox with a permanently displayed list (WXMSW only) |
Wx::CB_DROPDOWN | Creates a combobox with a drop-down list |
Wx::CB_READONLY | This prevents the User from editing the text and entering a choice of their own (behaves similar to a Wx::Choice). |
Wx::CB_SORT | Sorts the entries in the list alphabetically |
Wx::TE_PROCESS_ENTER | If enabled, it will enable the generation of the Wx::EVT_TEXT_ENTER event. |
A list of useful methods which can be used on the ComboBox widget.
Method | Description |
---|---|
find_string(string, case) | Searches for an Item that matches the given string, and returns it’s index position. |
get_count | Returns the number of Items in the ComboBox. |
get_current_selection | Returns the index number of the Item currently being selected. |
get_string_selection | Returns the string label of the currently selected Item. |
get_selection | Returns the index number of the selected Item. |
get_string(n) | Returns the Label of the Item at the given index position. |
set_selection(n) | Sets the selection to the Item at the given index position. |
set_string(n, text) | Sets the Label of the Item at the given index position to the give String. |
set_value(text) | Sets the text for the combobox text field |
A list of event connector methods which can be used for the ComboBox widget.
Event connector | Description |
---|---|
evt_combobox | Connects handler for Wx::EVT_COMBOBOX event which occurs when an item in the list of the ComboBox is selected. |
evt_text | Connects handler for Wx::EVT_TEXT which occurs when the text in the ComboBox is changed. |
evt_text_enter | Connects handler for Wx::EVT_TEXT_ENTER which occurs when RETURN is pressed in the ComboBox. |
evt_combobox_dropdown | Connects handler for Wx::EVT_COMBOBOX_DROPDOWN which occurs when the drop-down list of the ComboBox is displayed. |
evt_combobox_closeup | Connects handler for Wx::EVT_COMBOBOX_CLOSEUP which occurs when the drop-down list of the ComboBox closes. |
In this example we create a basic ComboBox widget and connect it to a method.
After creating the base code, we create a list of strings, which will be used as options on the ComboBox. As the initial value, we simply assign the first value in the array.
Lastly, we connect the Wx::EVT_COMBOBOX event to the combo_box_event
method, which prints out the label of the
currently selected item.
require 'wx'
class MyWindow < Wx::Frame
def initialize(title)
super(nil, title: title)
@panel = Wx::Panel.new(self)
values = ["Butter", "Milk", "Biscuits", "Cheese"]
cb = Wx::ComboBox.new(@panel, value: values.first, choices: values, pos: [50, 50])
evt_combobox(cb, :combo_box_event)
centre
end
def combo_box_event(e)
cb = e.get_event_object
puts cb.get_string_selection
end
end
Wx::App.run do
window = MyWindow.new("wxRuby ComboBox Guide")
window.show
end
The output of the above code when the 'Milk' options is selected:
-
-
Basic Guides
-
Widget Guides
-
Drawing Guides
-
Event Guides
-