This documentation is archived and is not being maintained.

Walkthrough: Inheriting from a Windows Forms Control with Visual J#

Visual Studio 2005

Visual J# provides you with the ability to create powerful custom controls through inheritance. Through inheritance, you are able to create controls that retain all of the inherent functionality of standard Windows Forms controls but also incorporate custom functionality. In this walkthrough, you create a simple inherited control called ValueButton. This button inherits functionality from the standard Windows Forms button, and will expose a custom member called ButtonValue.


The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

When you create a new project, you specify its name in order to set the root package, assembly name, and project name, and to ensure that the default component will be in the correct package.

To create the ValueButtonLib control library and the ValueButton control

  1. On the File menu, point to New, and then click Project to open the New Project dialog box.

  2. Select the Windows Control Library project template from the list of Visual J# Projects, and in the Name box, type ValueButtonLib.

  3. Click OK.

    The project name, ValueButtonLib, is also assigned to the root package by default. The root package is used to qualify the names of components in the assembly. For example, if two assemblies provide components named ValueButton, you can specify your ValueButton component using ValueButtonLib.ValueButton.

  4. In Solution Explorer, right-click UserControl1.jsl, then on the shortcut menu, click Rename. Change the file name to ValueButton.jsl.

  5. In Solution Explorer, right-click ValueButton.jsl, and select View Code.

  6. Locate the class statement, public class ValueButton, and change the type from which this control inherits from System.Windows.Forms.UserControl to System.Windows.Forms.Button. This allows your inherited control to inherit all the functionality of the Button control.

  7. Expand Component Designer generated code, locate the InitializeComponent method, and remove the line that assigns the AutoScaleMode property:


    This property does not exist in the Button control.

  8. On the File menu, click Save All to save the project.

    A visual designer is no longer available. Because the Button control does its own painting, you cannot modify its appearance in the designer. Its visual representation will be exactly the same as that of the class it inherits from, Button, unless modified in the code.


    You can still add components, which have no UI elements, to the design surface.

One possible use of inherited Windows Forms controls is the creation of controls that are identical to standard Windows Forms controls, but expose custom properties. In this section, you add a member called ButtonValue to your control.

To add the ButtonValue member

  1. In Solution Explorer, right-click ValueButton.jsl, and then on the shortcut menu, click View Code.

  2. Locate the class statement. Immediately after the brace ({), add the following code:

    // Visual J#
    // Creates the private variable that will store the value of your 
    // member.
    private int varValue;
    // Declares the methods to access or modify the member.
    /** @property */
    public int get_ButtonValue()
        return varValue;
    /** @property */
    public void set_ButtonValue(int value)
        varValue = value;

    This code sets the methods by which the ButtonValue member is stored and retrieved. The get_ statement sets the value returned to the value that is stored in the private variable varValue, and the set_ statement sets the value of the private variable by use of the value keyword.

  3. On the File menu, click Save All to save the project.

Controls are not stand-alone projects; they must be hosted in a container. To test your control, you must provide a test project for it to run in. You must also make your control accessible to the test project by building it. In this section, you will build your control and test it in a Windows Form.

To build your control

  • On the Build menu, click Build Solution.

To create a test project

  1. On the File menu, point to Add Project, and then click New Project to open the Add New Project dialog box.

  2. Select the Visual J# Projects node, and click Windows Application.

  3. In the Name box, type Test, and click OK.

  4. In Solution Explorer, right-click the References node for your test project, and then on the shortcut menu, click Add Reference to display the Add Reference dialog box.

  5. Click the Projects tab.

  6. Double-click the ValueButtonLib project to add the reference to the test project.

To add your control to the form

  1. In Solution Explorer, right-click Form1.jsl, and on the shortcut menu, click View Designer.

  2. In the Toolbox, double-click the ValueButton icon.

    An instance of ValueButton is displayed on the form.

  3. Right-click ValueButton, and on the shortcut menu, click Properties.

    You can examine the properties of this control in the Properties window. Note that they are identical to the properties exposed by a standard button.

  4. Set the ButtonValue property to 5.

  5. On the Common Controls tab of the Toolbox, double-click Label to add a Label control to your form.

  6. Move the label to the center of the form.

  7. Double-click valueButton1.

    The Code Editor opens to the valueButton1_Click event.

  8. Add the following line of code:

    // Visual J#
    label1.set_Text("" + valueButton1.get_ButtonValue());
  9. In Solution Explorer, right-click Test, and on the shortcut menu, click Set as Startup Project.

  10. On the Debug menu, click Start Debugging.

    Form1 is displayed.

  11. Click valueButton1.

    The number '5' is displayed in Label1, demonstrating that the ButtonValue member of your inherited control has been passed to Label1 by means of the valueButton1_Click method. Thus, your ValueButton control inherits all the functionality of the standard Windows Forms button but exposes an additional, custom member.