ActiveX in simple and comprehensible (II.)

Source: Internet
Author: User
Tags define object constant end error handling implement variable access
Active|activex What an Event

Events are used to inform programmers who use the control that something has happened so that programmers can handle it accordingly. Events are ubiquitous in VB programming, for example, when the mouse clicks on the control things will happen click events, text box content changes will occur change events, and so on. But this is different from these things, and it's different from InitProperties, ReadProperties, writeproperties the events mentioned earlier, which are already defined by the system. What we have to do is define our own events. The custom event must be declared in the Declaration section of the module, and then it can be triggered anywhere at any time, as long as you think there should be an event. For example, an event that represents a wink should be declared like this:

Public Event Blink ()

In parentheses, you can place any arguments you want to pass to the event. For a click event, this parameter may be clicked by the mouse at the x and y coordinates. And that's the rationale for just blinking, and you can't use the extra parameters.

To trigger an event, use the RaiseEvent method. For our blink, we associate it with a timer so that it blinks every now and then:

Private Sub Blinker_timer ()

RaiseEvent Blink

End Sub

In this way, an event is completed, without any skill or secret to speak of. Now for the original program, just write the appropriate event handling process, just like the following:

Private Sub Bodycontrol1_blink ()

Debug.Print, "Hee, I blinked again!"

End Sub

Here, most of the basic questions about ActiveX controls are spoken. The following will be some of the more advanced content, including picture and font properties, the About dialog box, Run-time read properties, and so on. Before you go on, take a look at the previous content and get a good understanding. Are you ready? OK, now it's getting more exciting.

First, let's take a look at the attributes, what else can be mined, such as colors or pictures and things like that.

Advanced property Design

The color value is stored in the long shaping variable, but if you just define a long shaping variable, obviously you can't get the color menu that is provided in VB:

This looks complicated, but it's not hard to do at all: all you have to do is declare the attribute as a ole_color type, as the following code does:

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 the previous mention of a third attribute process besides let,get? Now uncover the bottom: it is the Set property process, when you want to give the object variable value, we can not use let, but must use set to replace. This is because the object variable saved inside the control is not a copy of the object, but a reference to the object, which is a memory address. In order to distinguish from the replication of general variables, VB introduced the Set property process.

You might know that fonts and pictures are stored in objects, and that they all have their own dialog boxes to set the properties. To use these dialog boxes, all we have to do is declare the picture or font to be the image or the Font object type, and set the Set property procedure 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: there is nothing difficult ah. Indeed, it is so simple. Next, let's look at how to establish a read-only property for the control key. This is also a more content used in the design of controls.

Read-only property

The easiest way to do this is to not include anything in the Let/set attribute. But usually, this does not meet the requirements, sometimes, you may need a run-time read-only attribute. The so-called runtime-and the design-time counterpart, is that the allegation is eventually run in a development-completed program, while the design refers to the control being used in the process of developing the program.

To implement a Run-time read-only, you use the UserControl AmbientProperties object. It provides a lot of properties about the container of the control. There is a UserMode property in which the UserMode value is true when the control is in a run-time state. By providing detection of usermode during the let/get process, it is easy to realize that running is read-only property:

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

EndIf

M_multiline = New_multiline

PropertyChanged "MultiLine"

End Property

This code-protected property can only be modified at design time, and if you try to change it at run time, it will result in an "read-only at Run-time" error.

Similar to the AmbientProperties object, there are extender objects. For extender objects, it is necessary to have a good understanding before you start writing controls. The Extender object is an excuse for late binding that developers can use to access control properties that are maintained and controlled by the control container rather than the control itself. It provides properties, like Name,enable,left,top,height,width, and so on, which mostly appear in generic controls, before writing control properties, you should see if you already exist in the Extender object, and you can avoid duplication of effort, On the other hand, it is more efficient.

However, there are some problems with using the Extender object: Not all containers support access to the same extender property. Therefore, the choice of Extender object must be very careful, no this makes the control can only be used for a specific container. But if you are only developing controls for VB, then you need not have these concerns, try to use it.

It is also important to note that the Extender object cannot be accessed in the UserControl Initialize event, but it can be used in InitProperties and readproperties events.

Enumeration

Using enumerations is a common way to set properties in a control. It provides a drop-down list and several options for you to choose from. This facilitates user action without having to consider too many compatibility and error handling issues, simplifies property settings, and is more secure.

First, you must create an enumeration structure that is placed in the Declarations section. A series of constants and corresponding strings are then given. A constant value can be zero, or any integer that is larger than a constant value before it. If a constant is not given, then VB automatically assigns it a value, the first unspecified assignment is zero, and the other values are the preceding number plus one:

Public Enum Edirection

Left

right = 1

Up

Down

End Enum

To implement an enumeration property, you must create a standard property with a let and get property procedure. The trick here is to declare the type of the attribute as the 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

The only thing to note is that you can modify the list of properties at design time only, not at run time. Other, like reading, writing, saving and retrieving, are the same as using standard attributes.

That's all the tricks. Incredibly simple, isn't it? Does your control look more professional?

UserControl objects

ActiveX controls created with Visual Basic are always made up of any controls (called child controls or controls) that are placed on the UserControl by the UserControl object. Like a visual Basic form, the UserControl object has a code module and a visual designer. Place the constituent controls on the designer of the UserControl object, just like putting the control on a form



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.