UserGuide

Web Control Sets

From Xojo Documentation

Control Sets in web projects allow you to have a collection of controls of the same type that share a single set of event handlers. You create a Control Set by selecting a control and then changing the value for the Member Of property in the Inspector (located in the Control Set group on the Advanced or Gear tab).

To create a new Control Set for the selected control, choose New Control Set from the popup menu for the Member Of property. This creates a Control Set with the name of the selected control.

Web Control Set Event in Navigator

You add other controls to the Control Set by clicking on them and selecting the Control Set name to add them to. Choose the same name to make several controls part of the same set. You can only combine controls of the same type into the same Control Set.

Each control in the same Control Set gets a different index value, which is how you reference the control.

Container Controls (WebContainer) cannot be in a Control Set.

Sharing Event Handlers

When you have multiple controls on the web page that are part of a Control Set, the controls share their events. However, each event now has a new index parameter that you use to identify the control. Controls Sets are a handy way to manage multiple controls that share similar functionality.

To add a shared event handler, select any control in the Control Set and then choose Add Event Handler from the toolbar or menu. Select the Event Handler (or multiple event handlers) you want to add. They are now added in the section above the Control Set in the Navigator.

Web Control Set Parameter

When you click on the event handler, you'll see that is has an additional index parameter. Use this index parameter to identify which of the controls in the Control Set actually caused the event.

Identifying Controls

You can also refer to specific controls in a Control Set in other code by using the index parameter. For example, to change the Visible property to False of the 2nd Canvas in a Control Set, you could do this:

cnvTest(1).Visible = False

You can also remove a control in the Control Set by calling the Close method:

cnvTest(1).Close

If you refer to an index for which there is no control, you will get a NilObjectException. This means you can sometimes use that exception to determine the total number of controls in the Control Set:

Dim count As Integer
Try
Do
If cnvTest(count).Index > 0 Then
// Will raise an exception when count is greater than the
// number of controls in the Control Set.
End If
count = count + 1
Loop
Catch e As NilObjectException
// count is the number of controls in the Control Set
End Try

This presumes you have not removed any of the controls from the set by calling the Close method because this can leave gaps.