Skip to content

HowTo : RadioButton widget

Martin Corino edited this page May 6, 2024 · 2 revisions
     About      FAQ      User Guide      Reference documentation

wxRuby RadioButton Widget

This wxRuby guide demonstrates how to use the Wx::RadioButton 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.

wxRuby RadioButton syntax

The syntax for creating a RadioButton widget in wxRuby is as follows:

rb = Wx::RadioButton.new(parent, id, label, pos, size, 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.
  • label : String
    Optional text which appears on the widget. Empty by default.
  • pos : Array(Integer,Integer) or Wx::Point
    Optional coordinates for the position of the topleft corner of the widget. Default is Wx::DEFAULT_POSITION.
  • size : Array(Integer,Integer) or Wx::Size
    Optional dimensions of the widget. Default is Wx::DEFAULT_SIZE.
  • 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::RADIO_BUTTON_NAME_STR.

Note: As with all windows, keyword constructor alternatives for all arguments but the 'parent' argument are available. See here for more information.

RadioButton Styles

Available styles for the RadioButton widget:

Button Style Description
Wx::RB_GROUP Explicitly creates a new group of RadioButtons.
Wx::RB_SINGLE Creates a radio button which is not part of any radio button group.

RadioButton Methods

A list of useful methods which can be used on the RadioButton widget.

Method Description
set_label(string) Takes a string as parameter as the new label of the RadioButton.
get_label Returns a string of the current label of the RadioButton.
set_value(bool) Takes a boolean as parameter as the new value of the RadioButton.
get_value Returns a string of the current value of the RadioButton.
get_first_in_group Returns the First RadioButton of the group to which this RadioButton belongs to.
get_last_in_group Returns the Last RadioButton of the group to which this RadioButton belongs to.
get_next_in_group Returns the Next RadioButton of the group to which this RadioButton belongs to.
get_previous_in_group Returns the Previous RadioButton of the group to which this RadioButton belongs to.

Example Code

The first thing you need to do is create a few RadioButton widgets, with unique labels. The first RadioButton you create should have the Wx::RB_GROUP style. All RadioButtons created after this shall be in included in this group (wxRuby will implicitly create a group if you do not).

Next we bind the panel in which the RadioButtons are contained, to the Wx::EVT_RADIOBUTTON event and radio_buttons method. This means that any time a RadioButton in the panel is clicked, the radio_buttons method will be called.

Finally we create a Label on which the currently selected RadioButton’s label will be displayed. The radio_buttons method is in charge of changing text on the StaticText widget using set_label.

(e.get_event_object returns the object that triggered the event, in this case, the RadioButton)

require 'wx'

class MyWindow < Wx::Frame
  def initialize(title)
    super(nil, title: title, size: [400, 300])
    @panel = Wx::Panel.new(self)

    @rb1 = Wx::RadioButton.new(@panel, label: 'Cat', pos: [100, 100], style: Wx::RB_GROUP)
    @rb2 = Wx::RadioButton.new(@panel, label: 'Dog', pos: [100, 130])
    @rb3 = Wx::RadioButton.new(@panel, label: 'Tiger', pos: [100, 160])

    @panel.evt_radiobutton(Wx::ID_ANY) { |e| radio_buttons(e) }

    @label = Wx::StaticText.new(@panel, label: 'You have selected: None', pos: [100, 20])
    
    centre
  end

  def radio_buttons(e)
    rb = e.get_event_object
    @label.set_label_text("You have selected: #{rb.get_label}")
  end
end

Wx::App.run do
  window = MyWindow.new("wxRuby RadioButton Guide")
  window.show
end

The output after running the code and selecting the “Dog” option:

screenshot1

TIP
Alternatively, you can use the RadioBox widget, which is a faster way of creating multiple RadioButtons and managing them.

Clone this wiki locally