[Gambas-user] Accessing control in different form

Benoit Minisini gambas at ...2...
Tue Aug 12 10:21:10 CEST 2003


Le Mardi 12 Août 2003 05:35, Rob a écrit :
> Oops, sorry to have led you astray, I just tried that and it doesn't work.
>
> It says "unknown symbol 'textbox1' in class 'fmain'" which makes sense
> because textbox1 isn't a symbol, it's a child.
>
> But going me.parent!textbox1.text = "xx" gets you "Not an array".
>
> me.parent.children!textbox1.text = "xx" gets "unknown symbol 'children' in
> class 'control'".
>
> dim x as object
> x = me.parent
> x.children!textbox1.text = "xx"
>
> gets you "Not an array" again.
>
> Changing that last line to x.children("textbox1").text = "xx" gives you
> "Expected .ContainerChildren, got String instead".
>
> And you can loop through x.children using for each, but there doesn't seem
> to be any way to find out each child's name!
>
> Benoit, how would you do this?  Updating fields on one form from another is
> an everyday occurrence in VB.  All of the above examples fail the same way
> whether "Form controls are public" is set or not....
>
> Rob
>

Contrary to VB, Gambas classes can not refer to an automatically instanciated 
object.

There are many solutions to this problem:

1°) When you tell gambas that a form is the startup class, then it creates a 
instance of this form automatically and shows it. The problem is that I 
didn't make a property that allow to get a reference to this form.

I may make this property in a next version.
=> something like that: FMain.Form.Textbox1.Text = "hello"

2°) You can let the FMain instance pass its reference to FSecond.

in FMain:

 hSecond = NEW FSecond(ME)
 hSecond.ShowModal

in FSecond:

 PRIVATE $hForm AS Form

 PUBLIC SUB _new(hForm AS Form)
   $hForm = hForm
 END

 PUBLIC SUB Button1_Click()
   $hForm.textbox1.text="xx"
   ME.Close
 END

3°) Let the dialog return its data to its caller by using static variables.

in FMain:

 if FSecond.Dialog() THEN RETURN
 TextBox1.Text = FSecond.Result

in FSecond:

 STATIC PUBLIC Result AS String

 PUBLIC SUB btnOK_Click()
   Result = "xx"
   ME.Close
 END

 PUBLIC SUB btnCancel_Click()
   ME.Close(TRUE)
 END

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list