UsersGuide:Chapter 5:Creating New Instances of Controls On The Fly

From Real Software Documentation

Jump to: navigation, search

Creating New Instances of Controls On The Fly

There may be situations where you can’t build the entire interface ahead of time and need to create some or all of the interface elements on the fly. This can be done in Real Studio, provided that the window already contains a control of the type you wish to create. The existing control is used as a template. For example, if you wish to create a PushButton via code, there must already be a PushButton in the window that you can “clone.” Remember that controls can be made invisible, so there is no need for your template control to appear in the window. Once you have created a new instance of the control, you can then change any of its properties.

Suppose you need to clone a PushButton that is already in the window, named PushButton1.

To create a new PushButton control on the fly via code, do this:

  1. In the Properties pane for the PushButton, set its Index property to zero.
    When the new controls are created while the application is running, they will, be elements of a control array.
    The template control and its properties.

    In this example, a new PushButton will be created when the user clicks on the original PushButton.
  2. In the Action event of PushButton1, dimension a variable of type PushButton.
  3. Assign the variable a reference to a new control using the New operator and pass it the name of the template control.
    This example shows a new PushButton being created using an existing PushButton named PushButton1 as a template. When the new control is created, it is moved to the right of the template control:
Dim pb as PushButton
pb= New PushButton1 //clone of PushButton1
pb.Caption="Clone" //change caption just to be clear about this
pb.Left=me.Left+me.Width+10 //move it to the right

The Code Editor for PushButton1 should look like this:
Action event that clones PushButton1.

  1. Click the Run button.
  2. When the application launches, click the “Original” button.
    Real Studio creates a new PushButton to the right of Original. You can see it’s the variable “pb” from its Caption property. The window after the clone has been create should now look like this.
    The test application after creating the cloned PushButton.

If you click “Clone,” you will create another clone to the right of the first two, and so on.

Since any new control you create shares the same code as the template control, you may need to be able to differentiate between them in your code. You use the index property of the control to identify which control was clicked. For more information on using the Index parameter, see the following section, Sharing Code Among An Array of Controls in Chapter 5.

If your code needs to create different kinds of controls and store the reference to the new control in one variable, you can dimension the variable as being of the type of object that all the possible controls you might be creating have in common. For example, if a variable can contain a reference to a new RadioButton or a new CheckBox, the variable can be dimensioned as a RectControl because both RadioButtons and CheckBoxes are derived from the RectControl class. Keep in mind, however, since the variable is a RectControl, the properties specific to a RadioButton or CheckBox will not be accessible. If you need to see which classes of control are common to different controls, see the sections on each control in the Language Reference.


Personal tools