Friends who are familiar with VB will not be unfamiliar with the use of ActiveX controls, and many controls are greatly facilitated, but the only drawback is not to dynamically load the control, and the control must be placed on the form when designing. VB6.0 has been able to resolve this issue, but there is no clear explanation in the help, and there is no some key feature. Since the previous version can be dynamically created: If the object is created outside, you can use it in the set statement New keywords, createObject or getObject gives the object reference to the variable from the outside. If the object is a slave object, you need to use a high-level object to specify an object reference in the SET statement:
DIM XLAPP1 AS Excel.ApplicationSet XLAPP1 = New Excel.Application or DIM XLAPP As Object 'Defines the variable of the storage reference object. Set xlapp = creteObject ("excel.application") xlapp.visible = true
These syntax can easily cause misleading, think that the dynamic loading of ActiveX controls is also this method, may have friends like I use CreateObject to try countless times, but it is inspiring, I don't know what to think about Microsoft, dynamically load ACTIVEX control It is an extension control collection to implement, through actual exploitation, finally find a practical way to find a practicable method, below with a specific example. First, ActiveX control
The ActiveX control is the expansion section of the Visual Basic Toolbox. Use the ActiveX control method with controls that use other standards, such as the Checkbox control, exactly the same. After joining the ActiveX control in the program, it will become part of the development and operational environment and provide new features for the application.
The ActiveX component is interactive with the application through the client / server relationship and application. The client is an application code or component that uses component functions. The server is the object and its associated object. For example, suppose the application uses ActiveX controls to provide a standard employee form for company's various applications. The ActiveX control providing the employee form is the server, the application using this control is the client of the server.
Second, loading method
The Controls collection is expanded in VB6.0. In the previous version, the Controls collection lists the loaded controls on the form, which is useful in the iterative process. Controls collection identifies an intrinsic form-level variable called Controls. If an integer where optional Object is located, the keyword controls must be included. We usually use the following code in the window:
Text1.text = "Hello, World" can also be used to achieve the same purpose using the following code: Controls (1) .text = "Hello, World"
In addition to the original CLEAR, the REMOVE method (very strange, why Microsoft only provides these two methods in VB 5.0, without providing the Add method, because there is no ADD, these two methods are not used) Increase the add method, this method is to dynamically load control: controls.add (progid as string, name as string); Progid: ActiveX part progID, such as "vb.checkbox"; name: ActiveX part is loaded Name, such as "MyCheckbox"; To add a button called MyButton on the form, you can use: DIM OCONTROL AS Object 'Flats Class Variable Note: Here is Object Object Type Private Sub LoadControl () Set Ocontrol = Controls.add ("vb.commandbutton", "mybutton") Ocontrol.Left = 10 Ocontrol.top = 10 ocontrol.visible = true 'Make the control vougain End Sub
This is the standard grammar of VB6.0, which is also shown in routine, but although the method is realized by the dynamic loading of the control, the button is displayed on the form, but it can be pressed like a normal button, but the loaded control cannot be Pre-design response event code, such as: Event Sub myButton_Click () will be illegal, of course, the event to be responded can be encapsulated inside the control. In terms of programming, there is no big place, the purpose of developing the ActiveX control is to share the resource sharing, in order to be utilized by other developers, it is clear that the method is not allowed, and the relevant objects such as VBControls are clear. Finding that the VBCONTROLEXTENDER object is combined with EventInfo to provide event trap capture. The VBControleXtender object is especially useful for dynamic add controls. It provides a set of universal properties, methods, events to developers, one of its prominent features is to programming design Control events, familiar programming friends have a declaration of the event-based object statement: Dim WitHevents Objelemt As CeleMtvbControlExtender is no exception, the statement is the same, but it has a special event ObjectEvent (Info As EventInfo), it You can capture all events generated by the object using raiseevent, and the EventInfo data structure maps the value of the event's name, parameter number, and parameters. The VBControleXtender and EventInfo are combined with Select Case, which can be placed in advance to the events of different types of objects, and operate independently. Overwrite the above code to provide Click events:
DIM WITHEVENTS OCONTROL AS VBCONTROLEXTENDER 'Take the event declaration declaration After you can find that the object appears in the object drop-down box in the upper left corner of the code window, that is, the object has an event or method, its event has DragDrop, Dragover, Lostfocus, GotFocus, ObjectEvent, and Validate, where ObjectEvent is a generic event capture.
Private Sub LoadControl () Set oControl = Controls.Add ( "VB. CommandButton", "MyButton") oControl.Visible = True End SubPrivate Sub oControl_ObjectEvent (Info As EventInfo) Select Case Info.Name Case "Click" 'Click event' You You can add a handle Click event code msgbox "You press MyButton!" Case Else 'Other Event' Handle Unknown Events Here. End Selectend Sub
Of course, the standard control provided by Microsoft can be added to this method, and everyone will not doubt, but can it be developed? The answer is yes, we can use an actual example to explain. Third, an example description
Assuming a local network monitoring system, you need to switch between schematic and physical schematic, including a network structure, transmission resource, monitor host, etc., and physical schematic includes routers, devices, collectors, etc. To be consistent, if you double-click a device graphic, you will display real-time data for the device, etc. In order to simplify maintenance, the schematic and physical schematic diagram are encapsulated into an ActiveX control. Since each picture needs to load many graphic controls, the resources are large, and they cannot be Loading, you need to decompose it into two controls, first uninstall a control when switching, then load another control, so dynamically load ActiveX controls.
Schematic control is --Theory.ocx, corresponding project is CTheory;
Real map control is - Fact.ocx, corresponding project is cfact;
Note: For simplification, the license keyword is not set when designing the control.
The graphical object on the physical map control can be dragged, and the location information after dragging is notified to the form of the control through the event changePosition so that the next load can be displayed in the last position, the physical map and the schematic control have double-click event. The completed work is the same, and other events are ignored here.
Fourth, specific example
1, ready to work
Register the control theory.ocx, Fact.ocx (using regsvr32.exe registration);
Built Forms frmTest.FRM, place button cmdloadocx on the form, "Schematic"
2, declare the window-level variable and load function loading LoadControl
DIM WITHEVENTS OCONTROL AS VBCONTROLEXTENDER 'Map Simulation Control Object DIM MBLNTHEORY AS Boolean' Display Schematic
Private function loadControl (INTTYPE AS INTEGER)
If not ocontrol is nothing then 'first judges whether the object exists, if there is a Controls.Remove ("MapView")' uninstall control, this is very important end ifif intertype = 0 THENSET OCONTROL = Controls.Add ("ctheory. THEORY" , "MapView") Elseset Ocontrol = Controls.Add ("CFACT.FACT", "MapView") End IF
Ocontrol.height = 3500ocontrol.width = 6500ocontrol.top = 100oControl.visible = trueEnd FunctionPrivate Sub Form_Load () MBLNTHEORY = TrueEnd Sub
3, the button cmdLoadOcx write code Private Sub cmdLoadOCX_Click () If mblnTheory Then Call LoadControl (0) mblnTheory = False cmdLoadOCX.Caption = "physical map" Else Call LoadControl (1) mblnTheory = True cmdLoadOCX.Caption = "Schematics" End IfEnd Sub
4, for the event ChangePosition write code Private Sub oControl_ObjectEvent (Info As EventInfo) Select Case Info.Name Case "ChangePosition" MsgBox CStr (Info.EventParamters.Item (1) .Value) ":" _ CStr (Info.EventParamters. Item (2) .value) Case "dbclick" Double-click Processing Code Case Else 'End SelectensEnd Sub
Note: EventInfo's parameter EVENTPARAMTERS collection started with 1, in general, Microsoft's new set is usually starting at 1, while the old is starting with 0, such as rdoerroS.Item (0). 5, uninstall control front front window
Private Sub Form_unload (Cancel AS Integer) Controls.Remove ("MapView") Set Ocontrol = Nothingend Sub
6. Special attention to the ActiveX control added through the controls.add method must not be any reference to this control in this project, otherwise the system will be wrong.
Five, small knot
By using a dynamic load ActiveX control, use a large application to make a small application, make a variety of combinations of different ActiveX controls, making applications more flexible and change, such as your application to handle thirty access control, and a specific The user may only have one or two kinds of access control. It is not necessary to include all access controls to the app, and each access control can be packaged separately, only the components required to register, just like Windows custom installations. Microsoft's future technology basis is distributed component technology (DCOM), which will play the reuse of the code. You may wish to trial dynamic loading, may have the effect of surprise you!