[Gambas-user] Public Form Controls

Bruce Steers bsteers4 at gmail.com
Sun Jul 23 20:11:33 CEST 2023


On Sun, 23 Jul 2023 at 18:57, Bruce Steers <bsteers4 at gmail.com> wrote:

>
>
> On Sun, 23 Jul 2023 at 18:11, Demosthenes Koptsis <demosthenesk at gmail.com>
> wrote:
>
>> Hello,
>>
>> is there a way to access a second form controls like Form2.TextBox1 from
>> FMain without enable Public Form Control from options ?
>>
>>
> Sure
> you can use the Form[] _get method
> Form2["TextBox1"]
>
> that will return Control.class to properly acces TextBox.class do this...
>
> Dim tb As TextBox = Form2["TextBox1"]
> tb.Text = "hello"
>

Sorry that was too Brief.

accessing Form2["TextBox1"] will return a "Control" object so you can
access any general control properties like Width, Background, Name etc.

to access TextBox specific properties like Text, Placeholder etc use the
second method.

or you can make a function to access any control type use Object

Public Sub GetForm2Object(Name As String) As Object
  Return Form2[Name]

End

Then something like this will work...

GetForm2Object("TextBox1").Text = "This text"

The plus side of using Object is it works for ANY control and it's
properties.
the down side is the auto-complete only works if you specifically set the
object return type (Ie TextBox).

Public Sub GetForm2TextBox(Name As String) As TextBox
  if Form2[Name] Is TextBox Then Return Form2[Name]
  Error.Raise(Name & " is not a text box")

End

BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230723/d3fe225d/attachment.htm>


More information about the User mailing list