GUI Control Set

I have been playing a bit with Control Sets, that I think I need for just enabling/disabling 4 buttons ( I’m lazy… just to omit container controls overhead).

I understood it’s an array of identical classes and a easy way to deal with similar object behavior. So, I tried made a Control Set and assigned three different buttons to just enable/disable them instead of calling each one, pushing a button.

Control Set code for the three buttons was created as PushButton3 (could not edit Control Set! )

[code]Dim i as Integer = UBound(PushButton3)

[/code]
Returns an error, so I am not sure if it’s an array.

Any tutorials or advice would be great. I think this is a very interesting feature when working with just a few controls instead of ContainerControl ).

Docs can be much improved on this matter as kind feedback to Xojo… :confused:

Edited: I have found it’s not iteritable…

https://xojo.io/6e03a1f3a78e

Shouldn’t you be using Dim i as Integer = UBound(PushButton3(index)) as you want to reference the index number in the control set.

[quote=168846:@Amando Blasco]I have been playing a bit with Control Sets, that I think I need for just enabling/disabling 4 buttons ( I’m lazy… just to omit container controls overhead).

I understood it’s an array of identical classes and a easy way to deal with similar object behavior. So, I tried made a Control Set and assigned three different buttons to just enable/disable them instead of calling each one, pushing a button.

Control Set code for the three buttons was created as PushButton3 (could not edit Control Set! )

[code]Dim i as Integer = UBound(PushButton3)

[/code]
Returns an error, so I am not sure if it’s an array.

Any tutorials or advice would be great. I think this is a very interesting feature when working with just a few controls instead of ContainerControl ).

Docs can be much improved on this matter as kind feedback to Xojo… :/[/quote]

Control sets are not an array. They do not have an Ubound property, mainly because they are not necessarily sequential. It is possible to have member(1), then member(3), then member(7). You must count the number of members yourself, or use Window.Controls and loop through to count them.

Thx, I did anyhow a test and are not iteratable either… I know can be accessed like an array, but the type of class is weird to me.

for each lp as PushButton in PushButton3
    lp.Enabled = not True
  next

https://xojo.io/6e03a1f3a78e

[quote=168852:@Amando Blasco]Thx, I did anyhow a test and are not iteratable either… I know can be accessed like an array, but the type of class is weird to me.

for each lp as PushButton in PushButton3
    lp.Enabled = not True
  next

https://xojo.io/6e03a1f3a78e[/quote]

Here is how you do it :

for i as integer = 0 to 7 if PushButton1(i) <> nil then PushButton1(i).enabled = false end if next

[quote=168853:@Michel Bujardet]Here is how you do it :

for i as integer = 0 to 7
if PushButton1(i) <> nil then
PushButton1(i).enabled = false
end if
next[/quote]

Thx for the help Michel. Will use your advice in my project.

Anyhow I think the OOP implementation of this feature is … well… very vb’ish. I’m still trying to understand how it has been designed and implemented. Just curiosity about this feature… :slight_smile:

[quote=168854:@Amando Blasco]Thx for the help Michel. Will use your advice in my project.

Anyhow I think the OOP implementation of this feature is … well… very vb’ish. I’m still trying to understand how it has been designed and implemented. Just curiosity about this feature… :)[/quote]

I think I have always know them, if under the older name of control array. But as you saw they are not arrays, so I think Control Set better describes them.

I would not judge their level of OOP. They are extremely convenient for labels, since you can put all your labels in a single set and fold that, so they don’t litter the navigator.

One little tip : let us say you have removed one of the members and now they are not sequential. Select them all, go in the gear inspector, and select the name of the control set again. They will all be renamed sequentially.

Likewise, if you want to make a series of identical controls in a control set, select them all, and in the gear inspector pane, select “New Control set”.

If you want to take a member out of the set, simply rename it.

And I forgot the most important feature of control sets : they allow you to create members dynamically.

You mean like

[code] For i as Integer = 0 To ControlCount - 1

Dim lpControl as Object = Self.Control(i)

Select case lpControl
  
case isa PushButton3
  PushButton3(lpControl).Enabled = not PushButton3(lpControl).Enabled
  
End Select

Next
[/code]

Is that the normal way of accessing each member of PushButton3??

Thank you again Michel for your nice GUI handling advices. Will make good use of them :slight_smile:

Yes, that is the normal way of iterating a control set. The 2 main advantages of a control set are

  1. The controls share event code.
  2. You can create new members of the set on the fly.
    Using a control set the way you are is a little unusual. There are probably better solutions if you’re just concerned about toggling visibility. The main drawback I see to what you’re doing is since the buttons probably don’t all do the same thing, you have to combine all their Action code into one event using an if/then or select/case on the Index value. That’s going to be harder to maintain than having separate controls. And not very OOP either.

Great answer Tim!

Toggling were just an example, I would like to iterate trough the Control Set objects and handle them. I drop 4 same behavior classes and thought it would be OOP workable but seems it is not yet, needs some work to be as I wish.

About the Action code, I would like to call a superclass method via AddHandler or delegate when needed.

Thx @Tim Hare for your answer!

Maybe some Xojo devs can tell me more about this feature.

Hm… Events are shared among members.

I have just uploaded a new example to my example. I can get an Event called from all Control Set instances.
Have a look at PushButton3 Set (Action) Event…

lhttps://xojo.io/6e03a1f3a78e

[quote=168898:@Amando Blasco]Hm… Events are shared among members.

I have just uploaded a new example to my example. I can get an Event called from all Control Set instances.
Have a look at PushButton3 Set (Action) Event…

lhttps://xojo.io/6e03a1f3a78e[/quote]

I do not know why you find control sets not OOP. I tend precisely to use them because they are compact, and lend themselves to be reused. One object representing a whole collection of controls seems to me very cool.

There is one downside to control sets, though. I would like to treat them as one object, and for instance duplicate PushButton3 set. But if I hit Command-D while PushButton3 set is selected, it simply adds a new member. I do not quite know if it is a bug or intended, but THAT is not very OOP. Worse, I tried to copy it and paste it back, it still adds a member. The only way I found to duplicate a control set is by copying it to another project, rename it there, copy it and paste back.

Your project shows well what I think is a benefit of control sets, especially for buttons doing pretty much the same thing : one event.