[fxruby-users] FXTextField.connect(SEL_CHANGED)

Lyle Johnson lyle at lylejohnson.name
Thu Jul 29 15:09:21 EDT 2010


On Thu, Jul 29, 2010 at 12:01 PM, Ralph Shnelvar <ralphs at dos32.com> wrote:

> The following does not seem to work worth a damn ...
>
>     @v_file_FXTextField.connect(SEL_CHANGED|SEL_COMMAND) do |sender, sel,
> data|
>       v_model.filename = sender.text.strip
>     end
>
> Is there a way to do what I want which is to have a single connect do two,
> uh, connections?

There's no way to have a single call to connect() do what you want;
each call to connect() wires up exactly one message handler.

So you have at least a couple of options here. Because the block's
body (the stuff between "do" and "end") is so short anyways, I'd
probably just write out the two statements and call it a day. But if
it were something more involved, I'd probably pull it out into a
separate method, e.g.

    def update_model_filename(sender, sel, data)
      v_model.filename = sender.text.strip
      # ... possibly a lot more code that we don't want to duplicate ...
    end

and then call that from the connect() statements' blocks, i.e.

    @v_file_FXTextField.connect(SEL_CHANGED) do |sender, sel, data|
      update_model_filename(sender, sel, data)
    end
    @v_file_FXTextField.connect(SEL_CHANGED) do |sender, sel, data|
      update_model_filename(sender, sel, data)
    end

Along these lines, there's a two-argument form of connect(), where you
pass in some "callable" object as the second argument. So we could
more compactly write those two connect() statements as:

    @v_file_FXTextField.connect(SEL_CHANGED, method(:update_model_filename))
    @v_file_FXTextField.connect(SEL_COMMAND, method(:update_model_filename))

Here, the "method()" method (part of the Ruby core) takes a symbol as
its input and returns a Method object.

Hope this helps,

Lyle


More information about the fxruby-users mailing list