Elementary FXDialogBox query

  • Thread starter Nigel Wilkinson
  • Start date
N

Nigel Wilkinson

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
 
L

Lyle Johnson

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

I have a class set up as follows

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
 
N

Nigel Wilkinson

<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
 
R

Rich

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" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Cc: "Lyle Johnson" <[email protected]>; <[email protected]>
Sent: Sunday, June 06, 2004 5:47 AM
Subject: Re: Elementary FXDialogBox query
 
L

Lyle Johnson

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.

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.
;)

Hope this helps,

Lyle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top