Export (0) Print
Expand All

Creating a Control

Visual Studio 6.0

Using WFC, you can create your own controls. A control created with WFC extends com.ms.wfc.ui.UserControl, which in turn extends com.ms.wfc.ui.Form; this allows you to use the Forms Designer to create the control's user interface.

Note   Before you use the following procedure to create a control, close any projects that you may already have open. (On the File menu, click Close All.)

To create a control

  1. On the File menu, click New Project.

  2. On the New tab, expand the Visual J++ Projects folder and click Components. Then select the Control icon.

  3. In the Name box, enter a name for your project.

  4. In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.

  5. Click Open. A collapsed view of your project appears in http://msdn.microsoft.com/en-us/library/aa243138(v=vs.60).aspx .

  6. In Project Explorer, expand the project node. A file with the default name of Control1.java has been added to your project.

Note   Renaming this file does not rename the associated class in the source code, and vice versa. You must manually change all instances of the old name. (Note that you can create an empty project and then add a control class with the Control template. This two-step process allows you to name the control before it is created.)

Viewing Code in the Text Editor

To view the source code that was generated, right-click Control1.java in Project Explorer and click View Code on the shortcut menu. The code in the initForm method represents the modifications you make in the Forms Designer. The nested class ClassInfo specifies the properties and events of your control. (To modify the ClassInfo, you can use the http://msdn.microsoft.com/en-us/library/aa265392(v=vs.60).aspx .)

Note that the Javadoc comments for the Control1 class provide information about how to expose your class as an ActiveX control. For more information, see Setting COM Classes.

Modifying the Control in the Forms Designer

The following procedures show how to use the existing WFC controls to design your control. This example adds an edit box and a horizontal scroll bar to your control; whenever the scroll bar is scrolled, the edit box displays the scroll bar's current position.

To add existing controls to your control

  1. To open your control in the Forms Designer, double-click Control1.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 an edit box to your control, click the Edit control in the Toolbox and then click your control's design surface.

  4. To add a horizontal scroll bar to your control, click the HScrollBar control in the Toolbox and then click your control's design surface.

  5. Position the edit box in the upper-left corner of your control's design surface, and move the scroll bar below the edit box. (To reposition an item, select and drag it with the mouse.)

  6. Resize your control's design surface around the edit box and scroll bar to eliminate extra space. (To resize the design surface, select it and drag the resize handles.)

To add an event handler

  1. Click the Events toolbar button in the Properties window. (If the Properties window is not displayed, click Properties Window on the View menu.)

  2. To display the events of the scroll bar, either select the scroll bar on your control's design surface or select the name of the scroll bar object from the drop-down list in the Properties window.

  3. Find the scroll event and enter displayPosition for the name of the method that will handle the event. When you press ENTER, the Text editor opens to an empty event handler named displayPosition.

  4. Inside the definition of the displayPosition event handler, add the following line of code:

    edit1.setText("Position = " + HScrollBar1.getValue());

For more information about modifying your code in the development environment, see http://msdn.microsoft.com/en-us/library/aa242915(v=vs.60).aspx .

Building the Control

When you build your project, your control is automatically made available to the Toolbox.

To build the control

To add the control to the Toolbox

  1. Right-click the Toolbox and click Customize Toolbox on the shortcut menu.

  2. Click the WFC Controls tab and select the name of your control.

  3. Click OK.

Adding the Control to a Form

To use your control, you can add it to a WFC form.

To add a form to your project

  1. In Project Explorer, right-click the name of your project. Point to Add on the shortcut menu and then click Add Form.

  2. Select the Form icon.

  3. In the Name box, enter a name for the Java class.

  4. Click Open. A file with the default name of Form1.java has been added to your project and is opened in the Forms Designer.

To add your control to the form

  1. Find your control in the Toolbox. To scroll through the list of controls, you can click the up and down arrows on the Toolbox tabs.

  2. Click your control in the Toolbox and then click the form to add the control.

To build and run the form

  1. On the Build menu, click Build. (If you receive any compilation errors or messages, correct the errors and rebuild your project.)

  2. To run the form, click Start on the Debug menu.

    Note   Because you are running your project for the first time, and because your project contains two .java files, the Project Properties dialog box is displayed. On the Launch tab, select the Default option and specify that Form1 should load when the project runs. Click OK. (For more information about project properties, see http://msdn.microsoft.com/en-us/library/aa265172(v=vs.60).aspx .)

  3. Click the ends of the scroll bar in your control. The edit box displays the current scroll position.

  4. To close the form, click the Windows Close button located in the upper-right corner of the form.

For more information about creating WFC controls and applications, see http://msdn.microsoft.com/en-us/library/aa266233(v=vs.60).aspx and http://msdn.microsoft.com/en-us/library/aa264872(v=vs.60).aspx .

Show:
© 2014 Microsoft