Skip to content

HowTo : ToggleButton widget

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

wxRuby ToggleButton Widget

This wxRuby guide demonstrates how to use the Wx::ToggleButton widget with it’s various features and functions. A complete list of options will be included here together with code examples for your convenience.

wxRuby ToggleButton syntax

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

tb = Wx::ToggleButton.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::CHECK_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.

ToggleButton Methods

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

Method Description
get_value Returns a bool (true/false) to reflect the current state of the ToggleButton.
set_value(bool) Sets the ToggleButton to the state mentioned in the parameters.

Example Code

A simple example where we create two ToggleButton widgets, and set one of them to the On state.

require 'wx'

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

    toggle1 = Wx::ToggleButton.new(@panel, label: 'Off', pos: [50, 50])
    toggle2 = Wx::ToggleButton.new(@panel, label: 'On', pos: [150, 50])

    toggle2.set_value(true)
    
    centre
  end
end

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

The output, where you can see both the two ToggleButton’s in different states:

screenshot1

Clone this wiki locally