|
A Quick Example: Build the AXYesNo
In this quick example, you build an ActiveX control that can be inserted on a Web page.
The ActiveX control has three constituent controls, two command buttons, and one label. In
this example, you toggle the caption of the label by clicking the two command buttons. To
build this control, follow these steps:
- Start Visual Basic 5. If it is already running, choose File, New Project.
- In the New Project dialog box, select ActiveX Control Project. A new project should
start with a UserControl object named UserControl1.
- Change the Name property of the UserControl
to AXYesNo. If the Properties dialog box is not visible, select View, Properties Window.
Find the property name property and change it by typing AXYesNo, as in Figure 3.1.
Figure 3.1
Set the control name property to "AXYesNo" in the properties window.
- Add two command buttons to your UserControl. To add controls to the UserControl, click the CommandButton icon in the toolbox, then
click and drag the mouse over the control drawing area, as in Figure 3.2.
Figure 3.2
Draw two command buttons on the control drawing area by selecting the CommandButton
icon and then clicking and dragging on the drawing area.
- Position the two CommandButtons so that you can see both of them. To move a
CommandButton, click and drag it across your UserControl. You
can also resize it by dragging the sizing blocks located on each of its corners.
- Name one of the command buttons cmdYes and the other cmdNo.
- Set the caption of cmdYes to Yes and the caption of cmdNo to No.
- Add a label control to the UserControl object and name it
lblDisplay.
- Add code to the click event of cmdYes to have lblDisplays caption property say
yes. To go to the code window, click cmdYes twice. The cursor should be set in the Private Sub cmdYes_Click() event. Add the code shown in Listing
7.1.
Listing 3.1 - AXYESNO\AXYESNO.CTL - cmdYes Code
Private Sub cmdYes_Click()
lblDisplay.Caption = "Yes"
End Sub
- Add code to the click event of the cmdNo button to have it change lblDisplays
caption property to No. To navigate between object events inside of the code window, go to
the object list and select the cmdNo object. Verify that the event you are looking at in
the procedure list is the click event. Add the code shown in Listing 7.2.
Listing 3.2
- AXYESNO\AXYESNO.CTL - cmdNo Code
Private Sub cmdNo_Click()
lblDisplay.Caption = "No"
End Sub
- Open the Project, Project1 Properties dialog box and name the project
AXEYesNo and provide a project description, as shown in Figure 3.3.
Figure 3.3
Use the Project Properties dialog to provide the control with a project name and
description.
- Build your control by selecting the File, Make AXYesNo.ocx menu entries.
- Save your project to its own folder that you name ..\Samples\AXYesNo.
Save the UserControl with the name AXYesNo.ctl. Save the
project with the name AXYesNo.vbp.
- Test the control in a test project. Choose File, Add project. From the Add
project dialog box, select Standard EXE. Inside Visual Basic, close the UserControl object window. You now see that the user control object
you made AXYesNo is now available in the toolbox, as seen in Figure 3.4. Add a AXYesNo to
Form1, as in Figure 3.5. You can now test your control by selecting Run, Start.
Click each of the command buttons and verify that they work correctly, as in Figure 3.6.
Stop the project by choosing Run, End.
Figure 3.4
Once you close the window containing the control you are developing, it will become
available in the toolbox for use in another Visual Basic application.
Figure 3.5
The control you have built may be placed on a regular Visual Basic window.
Figure 3.6
When you run the standard Visual Basic project, you can verify that your control
works correctly.
- Test your ActiveX control from an HTML container using the Setup Wizard. Close Visual
Basic. From the Visual Basic program group on the Start menu, select Application Setup
Wizard. On the Select Project dialog box, select ...\AXYesNo\AXYesNo.vbp
and check Create Internet Download Setup, as in Figure 3.7.
Figure 3.7
To build the necessary files for use in a HTML document, you will need to use the
Application Setup Wizard to create an Internet Download Setup.
- On the Internet Distribution Location dialog box, pick a location where you want your
distribution files to become available, as in Figure 3.8.
Figure 3.8
You need to specify a location for the Setup Wizard to build the Internet Download
files.
- On the Internet Package dialog box, pick Use Alternate Location for run time
components but do not give a location, as in Figure 3.9. A blank will put the files with
the other runtime files.
Figure 3.9
You can specify whether the Visual Basic runtime files (and other necessary files)
will be downloaded from the Microsoft Web site, the same Web site as your control, or a
third Web site.
- On the Internet Package dialog box, click the Safety button. On the Safety dialog
box, specify that your control is Safe for initialization and Safe for
scripting (well explain these settings toward the end of the chapter), as in Figure
3.10.
Figure 3.10
By marking your control as safe for initialization and scripting, you are placing
your guarantee in your control that it can not harm the users computer, even if
being used in HTML documents that you did not build.
- Move forward through the ActiveX Server Components dialog. In the File Summary dialog,
the Setup Wizard will show you a summary of the files that will be included in the
package, as in Figure 3.11.
Figure 3.11
The File Summary dialog box shows you what files will be included in the Internet
download files that will be packaged for including on a Web site.
- Once the Setup Wizard has completed building the Internet Download files, open the
AXYesNo.HTM document in Internet Explorer to test your control in a Web browser, as in
Figure 3.12.
Figure 3.12
Once the Setup Wizard has completed building the download files, it will create a
simple HTML file that you can open in Internet Explorer to test your control.
|