C # control development (4)

Source: Internet
Author: User
VII. Custom types for writing and displaying
There are three ways to edit the attribute window: 1. In some cases, you can edit the window as a string, and type conversion is implemented by typeconverter. 2. You can select a value from the drop-down list. 3. An omitted button provides other UI interfaces for editing values, such as filedialog and fontpicker. We have already talked about the string format. Next we will look at the drop-down list.

. NET Framework already contains several examples of drop-down lists, such as color, accessiblerole, and dock. We can see the specific implementation of the drop-down list.

Figure 4. drop-down list Editor

The implementation of the drop-down is also defined by typeconverter. If you look at the description of typeconverter, you can see that there are three virtual functions to implement this function: getstandardvaluessupported (), getstandardvalues () and getstandardvaluesexclusive (). To reload these methods, we can provide a pre-defined value list for the attribute. In fact, typeconverter implements the enumerated values in the drop-down list. The Property Window itself does notCodeTo edit the drop-down list, instead of using typeconverter.

For example, we have a familymember component that contains the relation attribute, allowing users to select the relationship between them and others. To make the interface more user-friendly during design, the attribute window should use a drop-down list to provide some common values, such as mother, father, daughter, and sister. In addition to common values, component users can also enter other string values that represent links.

Public class familymember: Component

{

Private string relation = "unknown ";

[Typeconverter (typeof (relationconverter), category ("details")]

Public String Relation

{

Get {return relation ;}

Set {This. Relation = value ;}

}

}

Internal class relationconverter: stringconverter

{

Private Static standardvaluescollection defaultrelations =

New standardvaluescollection (

New String [] {"mother", "Father", "sister ",

"Brother", "Daughter", "son ",

"Aunt", "Uncle", "cousin "});

Public override bool getstandardvaluessupported (

Itypedescriptorcontext context)

{

Return true;

}

Public override bool getstandardvaluesexclusive (

Itypedescriptorcontext context)

{

// Returning false here means the property will

// Have a drop down and a value that can be manually

// Entered.

Return false;

}

Public override standardvaluescollection getstandardvalues (

Itypedescriptorcontext context)

{

Return defaultrelations;

}

}

But how to make a more customized UI? We can use the uitypeeditor class. The uitypeeditor class includes methods that can be called by the attribute window when displaying or editing properties (such as the drop-down list and omitted buttons.

Some attribute types similar to image, color, Font. name will have a small graphical representation on the left of the attribute value, which is implemented by reloading the paintvalue method of uitypeeditor. When the attribute window defines the attribute value of the editor, it provides the editor with a rectangle and graphic ), they are all included in the paintvalue method's event parameter paintvalueeventargs. For example, we have a grade class that requires graphical representation. The following is our grade class.

[Editor (typeof (gradeeditor), typeof (system. Drawing. Design. uitypeeditor)]

[Typeconverter (typeof (gradeconverter)]

Public struct grade

{

Private int grade;

Public grade (INT grade)

{

This. Grade = Grade;

}

Public int Value

{

Get

{

Return grade;

}

}

}

When we enter an age, we can see a graph on the left.

Figure 5. Input age

It is not difficult to implement it. Note the editorattribute attribute assigned to the grade class, which is the following class:

Public class gradeeditor: uitypeeditor

{

Public override bool getpaintvaluesupported (

Itypedescriptorcontext context)

{

// Let the property browser know we 'd like

// To do M m painting.

Return true;

}

Public override void paintvalue (paintvalueeventargs PE)

{

// Choose the right Bitmap Based on the Value

String BMP name = NULL;

Grade G = (grade) PE. value;

If (G. value> 80)

{

BMP name = "best.bmp ";

}

Else if (G. value> 60)

{

BMP name = "OK .bmp ";

}

Else

{

BMP name = "bad.bmp ";

}

// Draw that bitmap onto the surface provided.

Bitmap B = new Bitmap (typeof (gradeeditor), BMP name );

PE. Graphics. drawimage (B, PE. bounds );

B. Dispose ();

}

}

As we mentioned above, uitypeeditor can select Properties from the drop-down menu and the pop-up dialog box. The following example will include such code. For more information, see the uitypeeditor. geteditstyle and uitypeeditor. editvalue methods and iwindowsformseditorservice interfaces.

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.