## wxChoice

A choice item is used to select one of a list of strings. Unlike a listbox, only the selection is visible until the user pulls down the menu of choices.

Derived from

wxControl
wxWindow
wxEvtHandler
wxObject

Include files

<wx/choice.h>

Window styles

There are no special styles for wxChoice.

See also window styles overview.

Event handling

EVT_CHOICE(id, func) | Process a wxEVT_COMMAND_CHOICE_SELECTED event, when an item on the list is selected.

See also

wxListBox, wxComboBox, wxCommandEvent

Members

wxChoice::wxChoice
wxChoice::~wxChoice
wxChoice::Append
wxChoice::Clear
wxChoice::Create
wxChoice::Delete
wxChoice::FindString
wxChoice::GetColumns
wxChoice::GetClientData
wxChoice::GetCount
wxChoice::GetSelection
wxChoice::GetString
wxChoice::GetStringSelection
wxChoice::Number
wxChoice::SetClientData
wxChoice::SetColumns
wxChoice::SetSelection
wxChoice::SetString
wxChoice::SetStringSelection

### wxChoice::wxChoice

wxChoice()

Default constructor.

wxChoice(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int n, const wxString choices[], long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = "choice")

Constructor, creating and showing a choice.

Parameters

parent

Parent window. Must not be NULL.

id

Window identifier. A value of -1 indicates a default value.

pos

Window position.

size

Window size. If the default size (-1, -1) is specified then the choice is sized appropriately.

n

Number of strings with which to initialise the choice control.

choices

An array of strings with which to initialise the choice control.

style

Window style. See wxChoice.

validator

Window validator.

name

Window name.

See also

wxChoice::Create, wxValidator

wxPython note: The wxChoice constructor in wxPython reduces the `n` and `choices` arguments are to a single argument, which is a list of strings.

wxPerl note: In wxPerl there is just an array reference in place of `n` and `choices`.

### wxChoice::~wxChoice

~wxChoice()

Destructor, destroying the choice item.

### wxChoice::Append

void Append(const wxString& item)

Adds the item to the end of the choice control.

void Append(const wxString& item, void* clientData)

Adds the item to the end of the combobox, associating the given data with the item.

Parameters

item

String to add.

clientData

Client data to associate with the item.

### wxChoice::Clear

void Clear()

Clears the strings from the choice item.

### wxChoice::Create

bool Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int n, const wxString choices[], long style = 0, const wxString& name = "choice")

Creates the choice for two-step construction. See wxChoice::wxChoice.

### wxChoice::Delete

void Delete(int n)

Deletes the item with the given index from the control.

Parameters

n

The item to delete.

### wxChoice::FindString

int FindString(const wxString& string) const

Finds a choice matching the given string.

Parameters

string

String to find.

Return value

Returns the position if found, or -1 if not found.

### wxChoice::GetColumns

int GetColumns() const

Gets the number of columns in this choice item.

Remarks

This is implemented for Motif only.

### wxChoice::GetClientData

void* GetClientData(int n) const

Returns a pointer to the client data associated with the given item (if any).

Parameters

n

An item, starting from zero.

Return value

A pointer to the client data, or NULL if the item was not found.

### wxChoice::GetCount

int GetCount() const

Returns the number of items in the choice.

### wxChoice::GetSelection

int GetSelection() const

Gets the id (position) of the selected string, or -1 if there is no selection.

### wxChoice::GetString

wxString GetString(int n) const

Returns the string at the given position.

Parameters

n

The zero-based position.

Return value

The string at the given position, or the empty string if n is invalid.

### wxChoice::GetStringSelection

wxString GetStringSelection() const

Gets the selected string, or the empty string if no string is selected.

### wxChoice::Number

int Number() const

Obsolescence note: This method is obsolete and was replaced with GetCount, please use the new method in the new code. This method is only available if wxWindows was compiled with `WXWIN_COMPATIBILITY_2_2` defined and will disappear completely in future versions.

Returns the number of strings in the choice control.

### wxChoice::SetClientData

void SetClientData(int n, void* data)

Associates the given client data pointer with the given item.

Parameters

n

The zero-based item.

data

The client data.

### wxChoice::SetColumns

void SetColumns(int n = 1)

Sets the number of columns in this choice item.

Parameters

n

Number of columns.

Remarks

This is implemented for Motif only.

### wxChoice::SetSelection

void SetSelection(int n)

Sets the choice by passing the desired string position. This does not cause a wxEVT_COMMAND_CHOICE_SELECTED event to get emitted.

Parameters

n

The string position to select, starting from zero.

See also

### wxChoice::SetString

void SetString(int n, const wxString& text)

Replaces the specified string in the control with another one.

Parameters

n

The zero-based index of the string to replace

text

The new value for this item

NB: This method is currently not implemented in wxGTK.

wxChoice::SetStringSelection

### wxChoice::SetStringSelection

void SetStringSelection(const wxString& string)

Sets the choice by passing the desired string. This does not cause a wxEVT_COMMAND_CHOICE_SELECTED event to get emitted.

Parameters

string

The string to select.

See also

wxChoice::SetSelection