[Gambas-user] How do I directly access the child controls in a panel container.

T Lee Davidson t.lee.davidson at ...626...
Sat Jan 10 22:27:32 CET 2015


On 01/10/2015 03:33 PM, Tobias Boege wrote:
> On Sat, 10 Jan 2015, T Lee Davidson wrote:
>>> If Panel1's containing Form is FForm, then you can do
>>>
>>>     FForm.Controls["TextBox1"]
>>>
[snip]
>>>
>>
>> Thanks for the elucidation, Tobi. But, I must be missing something.
>>
>> FForm.Controls["TextBox1"] returns a Control which does not have a Text property.
>>
>> .Parent ("Panel1.Parent") returns a Container which also does not have a Text property.
>>
>> If we do something like
>>
>> 	Dim hForm As Form
>> 	If Panel1.Parent Is Form Then hForm = Panel1.Parent
>>
>> we would have hForm.Controls["TextBox1"], which returns a Control with no Text property.
>>
>
> It returns an object *typed* as a Control. But if the object is actually a
> TextBox, say, you can "cast" that Control to a TextBox, with something like
>
>    Dim hControl As Control
>    Dim hTextBox As TextBox
>
>    hControl = FForm.Controls["TextBox1"]
>    If hControl Is TextBox Then
>      hTextBox = hControl
>      ' ...
>    Endif
>
> Since now the type of the hTextBox variable says "TextBox", and the object
> behind it has a Text property, this property can successfully be resolved.

That's great, Tobi. So then Martin can do something like:

   Dim hForm As Form
   Dim hControl As Control
   Dim hTextBox As TextBox

   If Panel1.Parent Is Form Then
     hForm = Panel1.Parent
     hControl = hForm.Controls["TextBox1"]
     If hControl Is TextBox Then
       hTextBox = hControl
       Print hTextBox.Text
     Endif
   Endif

I tried it. It works.


> As for the hijacking, my bad. But I didn't understand Martin's question
> either way, so let's pretend I thought this was relevant to the thread :-)

Lol. Okay. :-)


Lee
__________

"Artificial Intelligence is no match for natural stupidity."




More information about the User mailing list