It can be said that the Active platform represents the worldview of Microsoft. Using the ActiveX control to construct a mechanism that includes transaction monitoring from the user interactively and adapts to COM, to the Web server, all of which implement automated institutions, this is how Microsoft is planned. The desktop is called the ActiveX control based on COM-based components. The so-called ActiveX control is just compliance with a certain standard, a COM object interacting with the client. Like the components, the method and assembly of the electronic device can be used to construct an application with a manufactured component component. This is indeed a very fascinating technology. Although it is high, with ActiveX's wide application, more and more programming tools support to create an ActiveX control. This includes VB. Designing an ActiveX in VB is not more difficult than a normal VB application. This article is to teach you how to create an ActiveX control in VB. ActiveX is deeply outstanding (1) What is the ActiveX control? A simple answer is: Class with a graphical interface. You may have used class to program, which is a good way to implement code reuse. Of course, it also provides a lot of other benefits, which is not intended to be listed one by one. ActiveX control deepens this concept, let you write a widget (widget), then pack it, use it in the later program, or as a specific problem solution, to programmers Provide a simpler programming method. With ActiveX you can build a "composite" control, which is combined by several other controls. That is, in the ActiveX control, you can not only use the normal controls such as text boxes in VB, you can also use other ActiveX controls to form your own control, implement the features you want, and put It packs behind it. The composition of the ActiveX control is an ActiveX control consisting of some members of it: attributes, methods, and events. What kind of logic is there? Still use our body to do an example, see your body as an ActiveX control. This control should first be some attributes, such as: Your eyes open attribute. Obviously, this attribute value should have two cases: open or closed. When you use it, you can tell "Physical Control" to let this property change a new value to determine your eyes or close your eyes, or get the current attribute value to know the current physical condition. The method is the collective of the process and function in the control. It is not different from any other VB function and the process, you can also pass the parameters to them and return to the value you want. Suppose there is a class that describes the body, it should have "Look" method, and should be able to accept "Direction" parameters, then this method should be written as this: Public Sub Look (Direction As Integer "Select Case Direction Case 0 'Case 1' to the left Case 2 'to see Case 2' to see Case 3 'Back Select End Sub If you ask for return value, we will give an example of "reading". At this point, you must declare the "read" method to the function instead of the process public function read () AS String 'read = "Hello from the world" End Function in the body control, what we use or "LOOK" this way. When calling this method, you should go to "see" when you specify your eyes.
Similarly, we also specify another method, "read". This method will be used back to see what you see. Is this concept of "Method"? If you still can't understand, you can think about it: Your control is like a machine, you turn the control switch (input parameters), turn the handle (call method), then the machine's light is flashing, running Execute a method), finally from the machine (return value), it is like this. But wait ... If your machine wants to tell you what is going, what should it do? At this time, it should be "event". Finally, the body control will also provide a "blink" incident, which is used to inform the developer in the event of a blink of an eye, but do not have to know the internal work mode and why this event will be triggered. Let your hand, make a control. Don't think that it is a very difficult thing to make a control, although it is a point different from it and the general application. In the above, we already know that an ActiveX control consists of attributes, methods, and events, let's take a look at how to implement these things in the program, then connect them, combine it into a right machine. The simplest form of properties is a common variable declared with public declarations. For example, if you put the following code to your control engineering declaration: Public eyeopen as boolean, you can use this property in the code behind it. However, the facts that this attribute can do is too small. It is almost not working properly. Because any variation of the attribute value must be notified to save the control instance as needed to save the control instance. And because attribute values may be displayed in multiple places, it must be notified when the attribute value changes, so that it can synchronize the "Properties" window, "Properties Page", etc. Attribute values. Is it a bit difficult to understand? If you don't understand what to say, don't worry, you open a project first, add a control, try to change some properties of this control, come to see the above, is it understanding? Our current task is to make such a control. It can be seen that there are some differences between control programming and general programming. So how do you implement a few rows? This is to use the attribute process. When an attribute value is referenced or set, the property process is automatically called. Let's add such an attribute: Open the code window, click the "Tool" menu, select "Add Procedure" submenu, pop up the dialog box, fill in the process name "Eyeopen", then set the type " Attributes". When you have some sure, VB automatically creates a prototype of a property process, the code will be added to the code window: public property get eyeopen () AS VARIANT End Property Public Property Let Eyeopen (Byval VNewValue As Variant) end The remaining of Property is what you have to do, is a write attribute processing code, and fill this skeleton. You can see that VB actually wrote two, "get" and "let" attributes, is it a bit doubts? It is actually very simple: "get" is the process called when the attribute value is referenced, and "let" is called when the attribute value is written.
(In fact, there are third types of processes, we will encounter later, here to make a wake up) To make a property process work correctly, there must be a variable to save the true attribute value. Add the following line to the declaration section: Private M_EYOPEN AS BOOLLAN notes the 'm_' prefix, usually before the internal variable of the user control. It is time to fill the process frame now. First look at the Let process, this process has a parameter: The default is that the name is VNewValue, a Variant type. But we want a Boolean variable rather than Variant, because the eyes are only open and closed in our model. So change this parameter into "new_eyeopen as boolean". If you want this property as a read-only property, then don't change the Let process, let the process are empty. Otherwise, when you want to set this attribute value, you should do this: public property let EyeOpen (new_eyeopen as boolean) M_EYEOPEN = New_EYOPENPERTYCHANGED "eyeopen" "After you can write some related code, make the property settings Reaction End Property When you want to write attribute values in the program, you call this function, first save the property value inside a private variable, then execute the internal method of PropertyChanged, which is used to tell the Visual Basic property. And trigger a WriteProperties event. About this specific content, will be mentioned later. The GET process is simpler! It and the standard function have no two: public property get eyeopen () as boolean eyeeopen = m_eyeopen end protection finished these? Do not! I forgot the attribute value of the previously said it needs to be saved, so they can keep it when the programming session is converted. So how do you save and remove attribute values? This will use the PropertyBag object at this time. Using the PropertyBag PropertyBag object contains two methods: one is used to read, one is used to write. As mentioned earlier, when any attribute changes, the WriteProperties event of the control is triggered. At this time, you can save the attribute value in the property package. The following code implements this feature: Propag.writeProperty "EyeOpen", M_EYEOPEN, TRUE PROPBAG is an instance of the Propertybag object. The WriteProperty function contains three parameters, the first grid is the name of the property, which is the value to be saved, and the last parameter is the default value written without the user-defined attribute. Combine this default value of this and ReadPropertiy method, you can set the default value for the attribute value. If the property value is the same as the default, the property value will not really save. When you want to read, the ReadProperty function found that there is no content in the property package, it will return the default value. This saves some system overhead. It should be noted that the name of a member property must be passed as a string. When you use the control to process, do not change the name of this string, it must match the name of the state of this property. When the control is restarted, you must overload all saved attribute values. The ReadProperties event is triggered when data in the PropertyBag is read.
During this event, the task you have to do is loading the property value saved in the property package, calling the ReadProperty function to achieve a point. It should be noted that the default value settings in the two functions are required to be set. For example: m_eyeopen = propbag.readProperty ("EyeOpen", a method of synchronizing default is to set constants to replace specific values when needed, so you don't have to worry about problems. For example: Private const m_def_eyeopen = true "m_def_" prefix usually placed in front of the default. When the control is started, it is generally necessary to set the initial value for the property. This is usually implemented in the UserControl's initproperties event. This event occurs when the control instance is first physical, that is, when the control instance is placed on the form, it will not happen again during the future design period. UserControl has an initialize event, which is triggered every control instance is created. Obviously, the initialization process is too frequent if it is placed in the Initialize event, and there is no need. Design ActiveX controls need to be changed fundamentally. The key events that need to be responded are often different from general programming. Here is the code we want to implement: private sub usercontrol_initproperties () m_eyeopen = m_def_eyeopen end Sub is good, and now the part of the property is basically over, the next article is another member: method. [Splitpage] What an EVENT event is used to notify the use of the control, and something will happen, so that the programmer can process it accordingly. Events are all in VB programming, for example, when the mouse click control, the text box content changes changing, and so on. But here is different from these things, different from those mentioned earlier in INitproperties, Readproperties, Writeproperties, those that have been defined by the system. What we have to do is to define your own events. Custom events must be declared in the module's declaration section, and then you can trigger it whenever you feel that there should be an event. For example, a representative blinking event should be declared: public evenet blink () In parentheses, you can place any parameters you want to pass to the event. For a CLICK event, this parameter may click X and Y coordinates when clicked. And this is reasonable, it should be only blinking, and the additional parameters are used. Trigger the event to use the RaiseEvent method. For our blinking event, we linked it with a timer so that it was able to blink from time to time: private sub blinker_timer () raiseevent blink end Sub, this event is completed, no skill or secret . Now leave to the program, as long as the corresponding event processing process is written, just like this: private sub bodycontrol1_blink () debug.print "Hey, I blink again!" End Sub arrive here, most about ActiveX controls The basic problem is talked.
The later will be some more advanced content, including pictures and font properties, "About" dialogs, and running read properties, etc. Before you continue, look at the previous content, it is well understood. Ok? OK, now be more exciting course. First, let's first look at the properties, what else can be excavated, such as something like color or pictures. Advanced property design color value is stored in long shaped variables, but if you just define a long shape variable, it is obviously not able to get the color menu in VB provided: this looks complicated, actually doing It's not difficult: All you have to do is declare the property as an OLE_COLOR type, just like the code below: Public property get backcolor () AS ole_color backcolor = UserControl.backcolor End Property Public Property Let BackColor (ByVal New_BackColor As ole_color UserControl.backcolor = new_backcolor propertychanged "Backcolor" End Property Remember that the previous mentioned in addition to Let, Get, there is a third attribute process? Now unveiled the bottom: it is the set property process, when you want to assign a value to the object variable, you can't use letan, but you must replace it with Set. This is because the object variables saved inside the control are not the copy of the object, but only the reference to the object, that is, a memory address. For the copy storage of the same general variable, VB introduces the set attribute process. Perhaps you might know: fonts and images are saved in the object, and they all have their own dialogs to set the relevant properties. To use these dialogs, all we have to do this is to put pictures or fonts as a Picture or Font object type, and set the set attribute process for it. Public Property Get Font () As Font Set Font = lblText.Font End Property Public Property Set Font (ByVal New_Font As Font) Set lblText.Font = New_Font PropertyChanged "Font" End Property look at the code above, you are not thinking: Nothing is hard. It's true, it is simple. Below, let's see how to build read-only properties for the control button. This is also a more content used in the design of the control. The simplest method of read-only properties is not to join anything during the Let / SET property. But usually, this does not meet the requirements, sometimes you may need a time-only property. The so-called runtime - and the design correspondence, it is that the control is ultimately run in a program that is completed, and the design refers to the control used during the development program. To achieve time-only, you want to use the AmbientProperties object of UserControl. It provides a lot of properties about control containers. There is a USERMODE attribute that the usermode value is true when the control is running.
By providing detection of UserMode during the Let / GET process, it is easy to implement, the operation is read-only attribute: public property get multiline () as boolean multiline = m_multiline End Property Public Property Let MultiLine (Byval new_multiline as boolean) IF Ambient.userMode Then Err.raise 382 Exit Sub Err.raise 382 EXIT SUB Endif M_Multiline = New_Multiline PropertyChanged "Multiline" End Property End Property This code protection property can only be modified when designing, if you try to change it at runtime, you will generate "Property Is Read-Only AT Run-Time "error. There is also an Extender object similar to the AmbientProperties object. For Extender objects, it is necessary to understand well before starting the control. The Extender object is an advanced binding excuse, and the developer can access control properties maintained and controlled by the control container (rather than the control itself). It provides some properties, like Name, Enable, LEFT, TOP, Height, Width, etc. These appear in general controls, before writing control properties, you should see if there is already in the Extender object, on the one hand Avoid repeating work, and on the other hand, it is more efficient. But there are still some problems with the Extender object: not all containers support access to the same Extender property. So the use of Extender objects must be very careful, no this can only be used for a particular container. But if you just develop controls for VB, then you don't have to use these concerns, try it. It is also important to note that the Extender object cannot access it in the UserControl's initialize event, but you can use in Initproperties and Readproperties events. Enumeration When setting up properties in the control, use enumeration is a very common way. It provides a drop-down list and several options to let you choose. This makes it easy for the user's operation, but also considers too much compatibility and error handling problems, simplifies property settings, and safer. First, an enumeration structure must be established, placed in a declaration section. Then give a series of constants and corresponding strings. The constant value can be zero, or any integer that is larger than the front of it. If the constant is not given, the VB is automatically assigned, the first unspecified assignment is zero, the other values are previously one number plus one: public enum eDirection left right = 1 UP DOWN ENUM To implement enumeration properties, You must create a standard property with the LET and Get property procedures.
The trick here is the attribute type declaration of an enumerated type given: Public Property Get Direction () As eDirection Direction = m_Direction End Property Public Property Let Direction (ByVal New_Direction As eDirection) m_Direction = New_Direction PropertyChanged "Direction" End Property unique It should be noted that only a list of properties can only be modified when designing, not allowed to do this at runtime. Other, like reading, writing, saving, and retrieval, all of them are the same as using standard properties. This is all the skills. Unbelievable simple, is it? Does your control look more professional? The ActiveX control created by the UserControl object is always made up of any control (called sub-control or constituent control) selected by the UserControl object. Like the Visual Basic form, the UserControl object has code modules and visualized designers. Place the constituent controls on the designer of the UserControl object, just put the controls on the form. When placing an instance of an ActiveX control on the form, you create a UserControl object, as well as instances of all subcipes on the UserControl designer. These objects are encapsulated in the control. UserControl object has its own properties, methods, and events. For some properties, like BorderStyle, BackColor, etc., with its own write code to implement the BackColor property, it is better to use the UserControl object directly. This means that the BackColor property of the ActiveX control only needs to simply call the BackColor property of the UserControl object. Similarly, you can also design your control's Click event on the basis of the existing Click event of the UserControl object. In fact, in the previous content, we always deal with this object, but it has some special things, it should be more attention to the value. Most of its properties should be done when the control is designed. Here I explain some meaningfulness of more obscure and understanding, and some look don't know, there are not many expenses. Alignable properties When set to true, VB will automatically add a new property to the control: Align. This can arrange the position of the control in the container as placing the tool strip, and this also means that your control can be placed in the MDI program. CANGETFOCUS properties can determine if the user control can get focus at runtime. This value is false when you want to create a graphic control, or when running is an invisible control like Timer. It is to be noted that as long as the control contains at least one sub-control that is capable to receive focus, the CANGETFOCUS attribute cannot be set to false. If CANGETFOCUS is set to false, all of its subfections cannot be set to receive focus. The ControlContainer property defines whether a control can contain other controls as the control container as the Frames or Pictureboxes controls. The DEFAULTCANCEL property can add default and cancel properties for the control. After adding the Default and Cancel properties, the control can act as the standard command button.
That is to say, when the Default is set to TURE, pressing the back key to trigger the Click event of the control, and when the Cancel property is set to TURE, the ESC button will also trigger the Click event. You can know that the control is default control by checking the DisplayAsDefault property of the AmbientProperties object. Invisibleaatruntime enables you to build controls like Timer, which is invisible when running. Finally, the ToolboxBitmap property is used to specify icons on the VB toolbox. Microsoft's recommended size is 32x32, but the practice proves that 23x23 or 24x24 works better, and 32X32 is scaled to that, then it is displayed. The attribute of the attribute is a bit winding, and it is a bit of awareness. . VB allows for setting properties for each member of the control. This contains some higher levels of content that allows you to build more professional controls. Click Tools in the menu bar | Process properties, the following dialog box appears: You can enter a paragraph of the control in the description box, in which you can enter a help associated ID number in the help context identifier, put your control with A help file is associated, so when you point the property, press the F1 button to give the help content of this property. Use "Browse this page in property to use this page" field, you can assign the selected number to the custom properties page of the control. This way the user selects the member from the VB attribute browser, VB will direct the direct line. The Properties Category field enables properties to appear in a specific category in the "classification" mode of the properties browser of the VB. These categories include appearance, font, location, miscellaneous, etc., as long as one is selected. "Hidden Members" allows properties to be displayed in the properties browser, which is for some public members who don't want users to see public members, but remember, it is just hidden rather than being used. Using the "Not displayed in the property browser" can be removed from the property from the property browser when the control is designed (not at runtime). As a general principle, any permanent properties implemented with ReadProperties and WriteProperties should be displayed by the attribute browser. Conversely, any non-permanent attribute should not be revealed. "Default User Interface" is used to set the default attributes and methods of the control key. For example, because the CAPTION attribute is the default attribute of the Lable Control button, you can simplify the code, put the label1.caption = "hello" to: label1 = "Hello" finally the part of the data binding. In VB, the application of the database is very common. With MS's data access, you can easily link control properties and database fields. When the "Property Binding" and "Bind Properties to Data Field" are selected, this property can run in a standard binding control button, which means you can choose to choose this property. Database controls and field names. The last thing is here, almost most of the content related to the user control is finished. You can start to customize the right control for your own procedure, you can also let others share your results. Although the content is not a lot, it is not easy to eat completely. So, I will see more and practice, and I will learn. Of course, there is no perfect thing, the same for ActiveX controls. Reasonable use of user controls must pay attention to. The ActiveX control will easily become complicated. If you are not careful, what you pay is far more than you get. If you just want an object with properties and methods, then the class may be a better choice. User controls are relatively complex interface between applications, which takes up more resources. And in terms of programming, the control is more complex.