Creating a Control
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
On the File menu, click New Project.
On the New tab, expand the Visual J++ Projects folder and click Components. Then select the Control icon.
In the Name box, enter a name for your project.
In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.
Click Open. A collapsed view of your project appears in http://msdn.microsoft.com/en-us/library/aa243138(v=vs.60).aspx .
- 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
To open your control in the Forms Designer, double-click Control1.java in Project Explorer.
In the Toolbox, select the WFC Controls tab. (If the Toolbox is not displayed, click Toolbox on the View menu.)
To add an edit box to your control, click the Edit control in the Toolbox and then click your control's design surface.
To add a horizontal scroll bar to your control, click the HScrollBar control in the Toolbox and then click your control's design surface.
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.)
- 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
Click the Events toolbar button in the Properties window. (If the Properties window is not displayed, click Properties Window on the View menu.)
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.
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
Inside the definition of the
displayPositionevent 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
- On the Build menu, click Build. Any compilation errors or messages appear in the http://msdn.microsoft.com/en-us/library/aa239204(v=vs.60).aspx . (Double-clicking an error in the Task List moves the insertion point in the Text editor to the error.) Correct the errors and rebuild your control.
To add the control to the Toolbox
Right-click the Toolbox and click Customize Toolbox on the shortcut menu.
Click the WFC Controls tab and select the name of your control.
- 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
In Project Explorer, right-click the name of your project. Point to Add on the shortcut menu and then click Add Form.
Select the Form icon.
In the Name box, enter a name for the Java class.
- 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
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.
- Click your control in the Toolbox and then click the form to add the control.
To build and run the form
On the Build menu, click Build. (If you receive any compilation errors or messages, correct the errors and rebuild your project.)
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 .)
Click the ends of the scroll bar in your control. The edit box displays the current scroll position.
- 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 .