[Gambas-user] What is missed: pointer to control name

Rolf-Werner Eilert rwe-sse at osnanet.de
Tue Apr 24 13:13:25 CEST 2018


Am 24.04.2018 um 12:38 schrieb Tobias Boege:
> On Tue, 24 Apr 2018, Rolf-Werner Eilert wrote:
>> This is one thing I sometimes miss.
>>
>> Just an example from my current project.
>>
>> - I have GridView1, 2, 3, 4, 5.
>>
>> - There is an array of strings for the GridView titles.
>>
>> Now I want a ptr = titlearray.Find("Title3"), pointing to item no. 2. In
>> code, however, I have to do a
>>
>> Select ptr
>> Case 0
>> 	something "GridView1"
>> Case 1
>> 	something "GridView2"
>> case 2
>> 	something "GridView3"
>> etc.
>> End select
>>
>> If I could simply Something GridViewX (where X is my ptr - 1) it was much,
>> much easier.
>>
>> Do you agree, or am I completely wrong here?
>>
> 
> What you are looking for is Window._get [1]. In a test project, add Label1
> and Label2 with different Text properties, a SpinBox1 and a Button1. Then
> add the code:
> 
>    Public Sub Button1_Click()
>      Dim hLabel As Label
> 
>      hLabel = Me["Label" & SpinBox1.Value]
>      Print hLabel.Text
>    End
> 
> You can put the label number into the SpinBox, look up the constructed
> control name in your form, then use that object how you want. I have to
> "cast" it into a Label variable first, as the Text property lookup fails
> otherwise.
> 

Thank you for the example code, what I made from it runs flawlessly:

Private Sub droprahmenZeigen(kl As String)
Dim zeiger As Integer
Dim hGridView As GridView

   zeiger = klassenbez.Find(kl)
   If zeiger = -1 Then Return

   hGridView = Me["GridView" & CStr(zeiger + 1)]
   Drag.Show(hGridView)

   ' Select zeiger
   ' Case 0
   '   Drag.Show(GridView1)
   ' Case 1
   '   Drag.Show(GridView2)
   ' Case 2
   '   Drag.Show(GridView3)
   ' Case 3
   '   Drag.Show(Gridview4)
   ' Case 4
   '   Drag.Show(Gridview5)
   ' End Select

End


> That said, whenever this question comes up, I end up recommending to make
> a custom control. Relying on numbers in control names isn't pretty IMHO.

Oh yes, it is pretty ugly :)

Now that reminds me of the Tag property, maybe one could use it (in 
relation with some function that evaluates it). If I think of my above 
example, something like

Gridview1.Tag = klassenbez[0]
etc.
which would spare you getting a pointer 1, 2, 3... but could use the 
strings in klassenbez[] directly.

and then... have to think about it.

> If you have multiple numbered GridViews that you either want to treat all
> the same or couple into a single widget, it calls for encapsulation.

This is not the case here.

> A separate Control class *could*, depending on what you're doing, manage
> the GridViews better and take that code out of your main program. But you
> don't give enough context to estimate that.

That could be something for the Tag thing, but how to look up the 
strings? Would have to be some function like Find.

Regards
Rolf


More information about the User mailing list