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

Rolf-Werner Eilert rwe-sse at osnanet.de
Wed Apr 25 13:21:03 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.
> 
> 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.
> 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.
> 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.
> 
> Regards,
> Tobi
> 
> [1] http://gambaswiki.org/wiki/comp/gb.qt4/window/_get
> 

Hi Tobias,

I'm missing my first answer mail echo from the list. Hope you got it. If 
not, please tell me and I'll resend it.

Further to our discussion, I had the idea of using .Tag in some way, and 
this is what I found practical:

Public Sub GridView1_MouseDrag()

   With GridView1

     klasse = klassenbez[Val(.Tag) - 1]

When I copy this code into GridView2_MouseDrag() and 3 and 4 and so on, 
I won't have to take care that klassenbez[] is changed appropriately. 
But I still have to take care to change With GridView1 into GridView2 
etc., so it's only half way done.

Your solution would help here, but is it really worth making a new 
instance of a GridView just to look up its number for every MouseDrag? 
In my answer I posted yesterday, the function I included your solution 
in is only used seldomly, so I could live with it.

Regards
Rolf


More information about the User mailing list