CLR custom. Net Control creation (2) -- add custom attributes, methods, and events

Source: Internet
Author: User

The first section describes how to generate a simple custom control. A control has three basic features: attributes, methods, and events. The following describes how to add custom properties, methods, and events to a custom control.

The myviewcontrol is actually a Managed class that inherits system: Windows: forms: usercontrol. We only need to add the correspondingCodeOK.

Custom Attributes:

Public:

[Description ("Get control view handle")]

[Browsableattribute (false)]

Property intptr viewhandle

{

Intptr get ();

Void set (intptr value );

}

The keyword Property indicates a custom attribute. The returned value of intptr is viewhandle. Others are descriptions, which are listed one by one:

Browsableattribute: Indicates whether a property or event should be displayed in the property browser.

Categoryattribute: Describes the category of an attribute or event. When a category is used, the property browser groups attributes by category.
Descriptionattribute: When you select an attribute in the property browser, the text specified in the description will be displayed below the property browser to display the attribute function to the user.
Bindableattribute: Describes whether an attribute tends to be bound.
Defaultpropertyattribute: Specify a default attribute for the component. When you select a control on the Form Designer, the default attribute will be selected in the property browser.

Defaultvalueattribute: Set a default value for a simple type property.

Editorattribute: Specify a Special Editor for the attribute.
Localizableattribute: Indicates whether an attribute can be localized. Any attribute with this attribute will be persistently stored in the resource file.
Designerserializationvisibilityattribute: Indicates whether or how a property is persisted into the code.
Typeconverterattribute: Specifies a type converter for the attribute. The type converter can convert the attribute value to another data type.
Defaulteventattribute: Specify a default event for the component. When you select a control in form design, this event is selected in the property browser.

These attribute values are very important during design. If they are used well, they will bring great convenience to users.

Custom method:

The custom method is the same as the method in the C ++ common class, as long as we add the method to the myviewcontrol class, it will be OK,

Void test (){};

If you want to override the built-in method, add the virtual and override keywords. If you want to rewrite the refresh () function, then:

Virtual void refresh () override {};

Custom events:

The keyword delegate is used for custom events. The delegate is defined first, and then the function triggered by the event is written.

Public:

Delegate void mytesteven thandle (System: Object ^ sender, eventargs ^ e );

[Description ("Custom Event")]

Event mytesteven thandle ^ mytest;

Void onmytest (eventargs ^ e)

{

Mytest (this, e );

}

To trigger this event, you must respond to the mytest (eventargs ^ e) function and define an eventargs:

Eventargs ^ myargs = gcnew eventargs ();

Mytest (myargs );

In this way, this event is triggered. To pass your own parameters, You need to define a class that inherits from eventargs. This will be mentioned later.

Note:To see a custom event in the property browser, the event must be defined as public. The preceding method also applies.

 

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.