Designing the Layout of the Control

Use the Forms Designer to design the layout of a composite control. This process is identical to designing the layout of a form. You can add controls from the Toolbox, move and size controls on the surface of the UserControl, specify properties, and create event handlers.

This scenario demonstrates how to create a composite control that contains a GroupBox control and a CheckBox control in the upper-left corner of the GroupBox control. When the composite control is placed on a form, you can add controls within the GroupBox portion of the control. When the CheckBox control is unchecked, the controls within the composite control are disabled; when the CheckBox control is checked, the controls within the composite control are enabled.

To add controls to the UserControl

  1. To open your control in the Forms Designer, double-click GroupCheck.java in Project Explorer.

  2. In the Toolbox, select the WFC Controls tab.

    If the Toolbox is not displayed, click Toolbox on the View menu.

  3. To add a GroupBox control to the UserControl, click the GroupBox control in the Toolbox, and then click the UserControl design surface.

    A GroupBox control is added with the default name of groupBox1.

  4. To add a CheckBox control to the UserControl, click the CheckBox control in the Toolbox, and then drag the control over the upper-left corner of the GroupBox control you added previously.

    A CheckBox control is added with the default name of checkBox1. Ensure that the CheckBox control is contained within the GroupBox control.

To save screen space, it is important that you resize the UserControl. Because the UserControl can be resized, it is also important for the controls within the UserControl to resize when the dimensions of the UserControl change.

To set the size of the controls

  1. Resize the UserControl's design surface around the GroupBox and CheckBox controls to eliminate extra space. (To resize the design surface, select it and drag the resize handles.)

    The size of the UserControl's design surface is the default size of the control when it is added to a form.

  2. To allow the GroupBox control to be resized when the UserControl is resized, select the GroupBox control in the Forms Designer and set the anchor property to Top, Left, Right, Bottom.

    The GroupBox control is now anchored to the borders of the UserControl.

To provide the user of the GroupCheck control with a correct display, modify the properties of the GroupBox and CheckBox controls that were added to the UserControl.

To set the properties of the controls

  1. To remove the default caption of the GroupBox control, in the Properties window, select the text property and clear its contents.

  2. To set the default state of the CheckBox control to be checked, select the control in the Forms Designer, and set the checked property in the Properties window to true.

The next step is to add a property to your control.