. NET Component Programming (3) Property Editor

Source: Internet
Author: User

The problem of defaultvalueattribute left over in the previous chapter has not been found. I will continue to find the information and make up the information in time.
Today we will talk about the Component Property editor UI. In the vs environment, there are two types of property Editor: one is provided by Vs and the other is edited by component writers based on their own needs. In this chapter, we will make a simple learning of both of them. The main part of VS is the collection editor.
Let's take a look at some of the propertyattributes we didn't talk about in the previous chapter:
Editorattribute: Specifies the editor used by the property editor.
Designerserializationvisibilityattribute: Specifies whether the results obtained through property editor are saved in the code.
Localizableattribute: When you want to localize a form, any attribute with this attribute will automatically reside in the resource file permanently.
The code example is as follows. Note the description in the code.

Using system;
Using system. Collections. Generic;
Using system. collections;
Using system. text;
Using system. componentmodel;
Using system. componentmodel. design;
Using system. drawing;
Using system. Drawing. design;
Using system. Windows. forms;
Using system. Windows. Forms. design;

Namespace Components
{
// The list in VS 2005 is used in this example. If vs 2003 has friends, perform studentcollection on your own.
// As long as the property type is inherited from the ilist type, its property editor UI can be included in.
Public class demo3: Component
{
Public demo3 ()
{
_ Students = new list <student> (); // be sure to implement _ students in the constructor.
}

Private list <student> _ students;
Private string _ grade;

// Editor provided by.
// If no designerserializationvisibilityattribute is available, set a value for students. The value cannot be saved.
// You can comment out the designerserializationvisibilityattribute, set a value for students, close the vs environment, and re-open the project, and observe that the value of students is not saved.
[Designerserializationvisibility (designerserializationvisibility. Content)]
Public list <student> students
{
Get {return _ students ;}
Set {_ students = value ;}
}

// Customize the editor.
[Editor (typeof (gradeeditor), typeof (uitypeeditor), localizableattribute (true)]
Public String grade
{
Get {return _ grade ;}
Set {_ grade = value ;}
}
}

Public class student
{
Private int _ id;
Private string _ name;

Public int ID
{
Get {return _ id ;}
Set {_ id = value ;}
}

Public string name
{
Get {return _ name ;}
Set {_ name = value ;}
}
}

Public class studentcollection: collectionbase
{

}

Public class gradeeditor: uitypeeditor
{
[System. Security. permissions. permissionset (system. Security. permissions. securityaction. Demand)]
Public override system. Drawing. Design. uitypeeditoreditstyle geteditstyle (system. componentmodel. itypedescriptorcontext context)
{
// There are three types of uitypeeditoreditstyle: modal is pop-up, dropdown is drop-down, and none is none.
Return uitypeeditoreditstyle. Modal;
}

[System. Security. permissions. permissionset (system. Security. permissions. securityaction. Demand)]
Public override object editvalue (system. componentmodel. itypedescriptorcontext context, system. iserviceprovider provider, object value)
{
// Obtain the editor service, which can be used to create a pop-up window.
Iwindowsformseditorservice editorservice = (iwindowsformseditorservice) provider. getservice (typeof (iwindowsformseditorservice ));

// Context. instance -- you can get the current demo3 object.
// (Demo3) Context. instance). grade -- the current grade value can be obtained.

Frmgradeeditor dialog = new frmgradeeditor ();
Editorservice. showdialog (DIALOG );
String grade = dialog. Grade;
Dialog. Dispose ();

Return grade;
}
}
}

The property Editor works as follows:


 

Download source code

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.