. NET Component Programming (2) propertyattribute and eventattribute

Source: Internet
Author: User

I wrote a basic article last night. Some friends said it was too simple to write. I want to declare it here: Because I want to write a complete series of component programming, I started from the simplest one, in addition, many friends in the garden may never write components. Here, I hope my friends with component development experience can forgive me.
In the previous chapter, we created the simplest component. Today we will talk about the propertyattribute and eventattribute of component.
Eventattribute:
Browsableattribute, categoryattribute, descriptionattribute, and defaulteventattribute
Propertyattribute:
Browsableattribute, categoryattribute, descriptionattribute, defaultpropertyattribute, defavaluvalueattribute, editorattribute
, Designerserializationvisibilityattribute, typeconverterattribute, bindableattribute, localizableattribute
In this chapter, we will mainly talk about the red attribute above. The designer UI in the next chapter will talk about the blue attribute, and the purple attribute will not be explained.
The attribute is described as follows:
Browsableattribute: whether the attribute is visible in the Property Window.
Categoryattribute: the group to which the property or event belongs.
Descriptionattribute: Brief description of property or event.
Defaulteventattribute: Default event ,.
Defaultpropertyattribute: Default property. The selected component is selected by default in the Property Window.
Defaultvalueattribute: default value of property. The selected component is selected by default in the event window.

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. componentmodel;

Namespace Components
{
// Propertyattribute and eventattribute are placed on the property and event respectively and included in.
// Properpropertyattribute and defaulteventattribute must be placed on the Class header.
[Defaultevent ("customerlogout")]
Public Class Customer: Component
{
Private string _ id;
Private string _ sex;
Private int _ age;
Private string _ address;
Private datetime _ createtime;

// No categoryattribute or descriptionattribute.
Public String ID
{
Get {return _ id ;}
Set {_ id = value ;}
}

// This attribute is in the customer's details group. categoryattribute and descriptionattribute are also applicable to event.
[Category ("customer's details"), description ("customer's sex")] // you can write two attributes in one.
Public String sex
{
Get {return _ sex ;}
Set {_ sex = value ;}
}

[Category ("customer's details")]
[Description ("customer's age"), defaultvalue (20)]
Public int age
{
Get {return _ age ;}
Set {_ age = value ;}
}

[Defaultvalue ("Shanghai"), category ("customer's details")]
Public String address
{
Get {return _ address ;}
Set {_ address = value ;}
}

[Browsable (false)] // This property is invisible in the property window, And browsableattribute is also applicable to events.
Public datetime createtime
{
Get {return _ createtime ;}
Set {_ createtime = value ;}
}



Public sealed class customerlogineventargs: eventargs
{}
Public sealed class customerlogouteventargs: eventargs
{}

Public Delegate void customerlogineventhandler (Object sender, customerlogineventargs E );
Public Delegate void customerlogouteventhandler (Object sender, customerlogouteventargs E );

Public event customerlogineventhandler customerlogin
{
Add {}
Remove {}
}

Public event customerlogouteventhandler customerlogout
{
Add {}
Remove {}
}
}
}

Its property and event windows are as follows:

I have never used defaultvalueattribute. The address and age in the above Code didn't get defaultvalue when customer1 was created. I will find out the cause and add it in the next chapter. I also hope that you can tell me the cause.

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.