Elementary FXDialogBox query

Hi

I'm trying to use a dialog box in FXRuby.
I have a class set up as follows

class AddItemDialog < FXDialogBox
  def initialize(owner)
    super(owner, "Add entry dialog")
    FXLabel.new(self, "Entry Name")
    textField = FXTextField.new(self, 20)
    buttons = FXHorizontalFrame.new(self, LAYOUT_SIDE_BOTTOM|FRAME_NONE|LAYOUT_FILL_X|PACK_UNIFORM_WIDTH)
    FXButton.new(buttons, "&Accept", nil, self, ID_ACCEPT, FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
    FXButton.new(buttons, "&Cancel", nil, self, ID_CANCEL, FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
  end
  def create
    super
    show
  end
end

When invoked I get the dialog that closes when either of the buttons is pressed and returne a 1 or a 0 depending on which button is pressed.
What I want to do is capture the text in the text field so i tried

class AddItemDialog < FXDialogBox
  def initialize(owner)
    super(owner, "Add entry dialog")
    FXLabel.new(self, "Entry Name")
    textField = FXTextField.new(self, 20)
    buttons = FXHorizontalFrame.new(self, LAYOUT_SIDE_BOTTOM|FRAME_NONE|LAYOUT_FILL_X|PACK_UNIFORM_WIDTH)
    FXButton.new(buttons, "&Accept", nil, self, ID_ACCEPT, FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
    FXButton.new(buttons, "&Cancel", nil, self, ID_CANCEL, FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
    if self.execute != 0
      puts textField.text
    end
  end
  def create
    super
    show
  end
end

Now if the accept button is pressed it returns the text field entry but the dialog box stays visable, if the accept button is pressed again the dialog disappears and it returns 1.

Any pointers would be gratefully received.

Cheers
Nigel

I'm trying to use a dialog box in FXRuby.

OK.

I have a class set up as follows

<snip>

When invoked I get the dialog that closes when either of the buttons is pressed and return a 1 or a 0 depending on which button is pressed.

Yes, looks good.

What I want to do is capture the text in the text field so i tried

<snip>

Umm, no. You don't call execute() on the dialog box while you're still constructing it. Try something like this instead:

  class AddItemDialog < FXDialogBox
    # Add an accessor so we can get to the text field later
    attr_accessor :textField

    def initialize(owner)
      ... as before, but...
      @textField = FXTextField.new(...)
    end
    def create
      ... as before ...
    end
  end

and then invoke it like this:

  theDialog = AddItemDialog.new(someWindow)
  if theDialog.execute != 0
    puts theDialog.textField
  end

Hope this helps,

Lyle

···

On Jun 5, 2004, at 12:41 PM, Nigel Wilkinson wrote:

Thanks, I've now got it working. Out of curiosity what class does theDialog.textField return as I have had to use theDialog.textField.to_s to use it as a string.

Cheers
Nigel

···

--On Sunday, June 06, 2004 03:51:52 +0900 Lyle Johnson <lyle@knology.net> wrote:

On Jun 5, 2004, at 12:41 PM, Nigel Wilkinson wrote:

I'm trying to use a dialog box in FXRuby.

<snip>

Umm, no. You don't call execute() on the dialog box while you're still
constructing it. Try something like this instead:

  class AddItemDialog < FXDialogBox
    # Add an accessor so we can get to the text field later
    attr_accessor :textField

    def initialize(owner)
      ... as before, but...
      @textField = FXTextField.new(...)
    end
    def create
      ... as before ...
    end
  end

and then invoke it like this:

  theDialog = AddItemDialog.new(someWindow)
  if theDialog.execute != 0
    puts theDialog.textField
  end

Hope this helps,

Lyle

Referencing an FXTextField directly should se the FXTextField class.

FXTextFields have the read/write property 'text' that holds a string
representation of the information in the text field.

When you reference an FXTextField directly, but then call it's 'to_s'
method, it will give you the same value as it's text property.

Here's some sample code to show that...

def handle_change_in_text_field(sen,sel,data)
    puts "The text field now holds "+data+" in it's 'text' property"
end

@user_input=FXTextField.new(parent_container,50)
@user_input.text="Please put your name here"
@user_input.connect(SEL_COMMAND,method(:handle_change_in_text_field))

-Richard

···

----- Original Message -----
From: "Nigel Wilkinson" <nigel@waspz.co.uk>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Cc: "Lyle Johnson" <lyle@knology.net>; <fxruby-users@lists.sourceforge.net>
Sent: Sunday, June 06, 2004 5:47 AM
Subject: Re: Elementary FXDialogBox query

--On Sunday, June 06, 2004 03:51:52 +0900 Lyle Johnson <lyle@knology.net> > wrote:

>
> On Jun 5, 2004, at 12:41 PM, Nigel Wilkinson wrote:
>
>> I'm trying to use a dialog box in FXRuby.
>
> <snip>
>
> Umm, no. You don't call execute() on the dialog box while you're still
> constructing it. Try something like this instead:
>
> class AddItemDialog < FXDialogBox
> # Add an accessor so we can get to the text field later
> attr_accessor :textField
>
> def initialize(owner)
> ... as before, but...
> @textField = FXTextField.new(...)
> end
> def create
> ... as before ...
> end
> end
>
> and then invoke it like this:
>
> theDialog = AddItemDialog.new(someWindow)
> if theDialog.execute != 0
> puts theDialog.textField
> end
>
> Hope this helps,
>
> Lyle
>

Thanks, I've now got it working. Out of curiosity what class does
theDialog.textField return as I have had to use theDialog.textField.to_s

to

use it as a string.

Cheers
Nigel

When you define an attr_accessor for a Ruby class, e.g.

  class MyClass
    attr_accessor :foo
  end

it creates a new instance method named "foo" that will allow clients to get or set the value of that instance variable, e.g.

  anObject = MyClass.new
  anObject.foo = Bar.new
  x = anObject.foo

So for the dialog box class from your previous example, we had an attr_accessor named "textField", and in the class' initialize method we assigned an FXTextField object to it:

  @textField = FXTextField.new(...)

So when you call:

  anAddItemDialog.textField

it returns a reference to the FXTextField object. To find out about what methods you can call on an FXTextField (or any other FXRuby class), you can consult the on-line documentation at http://www.fxruby.org. For example, here's a direct link to the FXTextField class docs:

  http://www.fxruby.org/doc/api/classes/Fox/FXTextField.html

As shown in the "Attributes" section, the "text" method returns a String containing the FXTextField's text. Because FXTextField is so smart, it also does this when you call the text field's to_s() method. :wink:

Hope this helps,

Lyle

···

On Jun 6, 2004, at 6:47 AM, Nigel Wilkinson wrote:

Thanks, I've now got it working. Out of curiosity what class does theDialog.textField return as I have had to use theDialog.textField.to_s to use it as a string.

Thanks everyone

Nigel