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

Tobias Boege taboege at gmail.com
Tue Apr 24 12:38:23 CEST 2018


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

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list