. NET component Programming (3) Property Editor

Source: Internet
Author: User

The last chapter left the DefaultValueAttribute problem, still haven't found the problem, I will continue to find information, found will be in time to fill.

Today we talk about component

Property Editor UI, in the VS environment, there are two kinds of property Editor, one is VS, and one is rewritten by component writers according to their own needs. In this chapter we will do a simple learning, vs. the main speaking collection Editor.

Let's take a look at some of the propertyattribute 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 the property editor are saved in the code.

Localizableattribute: When a user wants to localize a form, any property with that attribute will automatically reside permanently in the resource file.

The code instance is as follows, please note the annotation 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


{


//This example uses the List<&gt in VS 2005, and if a friend of VS 2003 invites himself to do studentcollection.


//The property Editor UI can be taken with VS as long as the type of the property is inherited IList type.


public class Demo3:component


    {


public Demo3 ()


        {


_students = new list<student> (); Be sure to instantiate the word _students in the constructor.


        }





private list<student> _students;


private String _grade;





//vs band Editor.


//If there is no designerserializationvisibilityattribute, set a value for students and the value cannot be saved.


//Everyone can comment out the DesignerSerializationVisibilityAttribute, set the value of students, turn off the VS environment, reopen the project, and observe that the students value has not been saved.


[DesignerSerializationVisibility (designerserializationvisibility.content)]


Public list<student> Students


        {


get {return _students;}


set {_students = value;}


        }





//user Custom 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.ITypeDescrip Torcontext context)


        {


//Uitypeeditoreditstyle has three kinds, modal is pop-up, Dropdown is Pull-down, none is not.


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)


        {


//Get editor service, which can be created by its pop-up window.


IWindowsFormsEditorService Editorservice = (iwindowsformseditorservice) provider. GetService (typeof (IWindowsFormsEditorService));





//context. instance--can get the current Demo3 object.


//((DEMO3) context. Instance). grade--can get the value of the current Grade.





Frmgradeeditor Dialog = new Frmgradeeditor ();


editorservice.showdialog (Dialog);


String grade = dialog. Grade;


dialog. Dispose ();





return grade;


        }


    }


}

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.