| Topic: Projects Using Intrinsic and Professional ActiveX Controls Objective of this Lesson: The objective of this lesson is to create the following projects illustrating the use of Intrinsic and Professional ActiveX Controls.
ActiveX is a reusable and portable software component or control built according to what are called COM(Component Object Model) specifications. It is the brainchild of Microsoft. It is the practical realization of the philosophy, “write once use anywhere and everywhere”. It provides a building block solution to complex problems. We have already seen that VB-6 comes with certain intrinsic ActiveX controls which offer some functionalities Such controls can be used not only in VB, but also in other ActiveX compliant languages like VC++., VJ++, Delphi etc. Can you Create Your own ActiveX Controls? It is well known that any complex application is made of compatible combinations of some basic functionalities. If you have predefined ActiveX controls to provide these basic functionalities, VB-6 offers language support to package the basic controls to another new single ActiveX control thereby providing an ActiveX solution to the complex problem. In fact VB-6 has been developed essentially as a Control Creation Language. Possibly you can visualize a scenario when every complex application will be solved by a single user created aggregate ActiveX control. Visual Basic ActiveX Controls: When you start VB, the first window called the design window shows some 20 intrinsic controls like label, command button, option button etc., in the Toolbox. These controls cannot be removed from the Toolbox. The Professional Edition of VB provides some 34 more ActiveX controls which can be brought into the Tool box before using them. These controls can be added to or removed from the Toolbox. In order to view all the VB ActiveX controls select ‘New Project’ from the File Menu. You will get the New Project Menu as shown in the figure 1. Click on the ‘VB Pro Edition Control’. The Toolbox of the Design Window will now show the icons of all the ActiveX controls as shown in the figure 2. When you move the mouse pointer over the icon, the names of the controls will appear below them.
Adding ActiveX Controls to the Toolbox: In order to add one or more ActiveX controls to the Toolbox, select the item ‘Components’ from the Project menu. The Components Dialog Box will appear on the screen as shown in the figure 3. Select the required control by clicking on the rectangular box at the beginning. After selecting the required controls, click OK. The icons of the selected controls will now appear on the Tool box and you can use them just as you will use any other intrinsic ActiveX control.
Functions of Some important ActiveX Controls: We give below a list of some important ActiveX controls added in the Professional Edition of VB-6. This is in addition to the intrinsic controls already introduced in earlier versions.
1. Project using the Timer Control: A digital clock is a very simple but very useful application involving the timer control. This project requires a timer and two labels. The running time will be displayed in one label and the current date will be displayed in the other label. Timer is an Iintrinsic ActiveX control and so is already present in the Tool Box. Drag one timer and two label controls into the form. Give the labels appropriate size, font and border and set their caption property blank. Set the timer interval property as 1000(one second) and the Enabled property as True. The project at the design time looks as in the figure 4.
Entering the Event Codes: Open the code window by double clicking the Timer control and enter the code as shown in the figure 4a. Private Sub Timer1_Timer() Figure 4a. Running the Project: Select Start from the Run Menu. The clock will begin to run. The current date will be displayed in the other label. The run mode window is as shown in the figure 5. Another important point you notice is that the timer control does not appear in the run mode, and is invisible, but works only from behind.
Building BarChart Project: Bar Charts are very useful for displaying business data, say product sales versus years. VB has an ActiveX control named Microsoft Chart Control, to draw two dimensional and three dimensional bar charts. Controls used in this project: The MS Chart control is brought into the Toolbox from the component dialog box. If it is not available in the dialog box, click the browse button and select the same from the control files directory. The MSChart icon will appear in the tool box. It must be placed in a bitmap container. So a picture box control is first drawn into the form. Then the MSChart control is placed inside the picture box. Then a command button is placed in the form. It is given the caption and name as ‘setchart’. The design window will look as shown in the figure 6. The code window is opened by double clicking the setchart button and the code is entered as shown in the figure 6a. Save the form as chartfrm and the project as chartprj. Select start from the Run menu. You will have to enter 9 data values in response to prompts. The bar chart will appear as shown in the figure 7. Private Sub setchart_Click() Figure 6a
3. Project using Combo Box: In this project we use 3 combo boxes to set different values for principal, ratepercent and years and compute simple and compound interest for the different sets of values. Drag 7 command buttons, 7 labels, 2 text boxes and 3 combo boxes in the form, size them caption them and position them as shown in the figure 8. The different sets of values for principal, ratepercent and years can be added either at design time or during run time. If you want to add items during design time, set the list property. Check list in the property box, then add items, pressing ctrl + enter after each item. To enter items at run time you have to write codes. Items that are selected in the combo boxes can be deleted by the delete buttons.. Open the code window and enter the codes as shown in the figure 8a. Save and run the project and experiment with the different controls. A typical run mode window is shown in the figure 9.
Private Sub Command1_Click() Private Sub Command2_Click() Private Sub Command3_Click() Private Sub Command4_Click() Private Sub Command5_Click() Private Sub Command6_Click() Private Sub Command7_Click() Private Sub Command8_Click()
Summary: In this lesson we have described some three projects and learnt a few things about ActiveX controls and their uses. In the next lesson we will consider some more ActiveX Controls.
|