[fxruby-users] Push a button programmatically

Jeroen van der Zijp jeroen at fox-toolkit.org
Tue Sep 9 10:02:46 EDT 2008

On Tuesday 09 September 2008, Thilko Richter wrote:
> Hello,
> I have a dialog box with a FXTextField and two buttons(OK and Cancel).
> The dialog should be closed if on of the buttons is pressed, or the
> user hit "ENTER" in the textfield. My dialog is inherited from
> FXDialogBox.
> This are my two buttons:
> ...
>  @connect_button = FXButton.new(buttons, "Connect", :target => self,
> :selector => FXDialogBox::ID_ACCEPT, :opts => BUTTON_NORMAL |
>  @cancel_button = FXButton.new(buttons, "Cancel", :target => self,
> :selector => FXDialogBox::ID_CANCEL, :opts => BUTTON_NORMAL |
> ...
> and this is my textfield:
> ...
> @textfield = FXTextField.new(content, 10, :opts => LAYOUT_SIDE_RIGHT |
> ...
> How can I "activate" the button when the user hits enter inthe
> textfield? I already found some solution which says something like
> this:
> ...
> @connect_button.handle(nil, FXSEL(SEL_LEFTBUTTONPRESS, 0), nil)
> @connect_button.handle(nil, FXSEL(SEL_LEFTBUTTONRELEASE, 0), nil)
> ...
> to simulate a click on the button. Is this the best solution?
> Thx,

You can connect SEL_COMMAND from the FXTextField to the ID_ACCEPT of
the dialog [no need to write handlers at all, unless you want to
grab the value of the textfield & check it first].

When you do this, be sure to set the TEXTFIELD_ENTER_ONLY option on
the textfield; it causes a tab out-of the textfield NOT to generate

Also, you might want to set the focus on this textfield on the first
show of the dialog.

Other useful things for optimal dialog interaction: setInitial() which
is used to identify the widget which is to react to the Return Key when
the focus tabs around to widgets which don't interpret this key.

Note that a FXTextField becomes the default widget to handle KEY_Return
when focus lands on it, and the default reverts to the control identified
as the initial default in the dialogbox when focus leaves the text field.

	- Jeroen

More information about the fxruby-users mailing list