Writing and. NET Properties Window interactive Rad component (iv)

Source: Internet
Author: User
Tags bool
Interaction Vii. Writing and displaying custom types
There are three ways to work with edits in the Properties window: First, some situations can be edited as strings, and then typeconverter to implement type conversions. Two, you can display a drop-down list to select a value. Third, an ellipsis button provides other UI interfaces to edit values, such as FileDialog and Fontpicker. We've talked about the string form, and then we'll look at the Drop-down list.

The. NET framework already contains several examples of Drop-down lists, such as attributes such as Color,accessiblerole,dock. We can see the concrete implementation of the Drop-down list from the following figure.




Figure 4. Drop-down List Editor

The task of implementing the dropdown 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 (). Overloading these methods, we can provide a predefined list of values for attributes. In fact, the TypeConverter implements the enumeration values in the Drop-down list. The Properties window itself does not have the code to handle the editing of this Drop-down list, but only the TypeConverter method.

For example, we have a familymember component that contains the relation attribute, allowing the user to choose a relationship with another person. If you want to make the design-time interface friendlier, the property window should use a Drop-down list to provide some common values: such as Mother,father,daughter and sister. In addition to the common values provided, component consumers can enter other string values that represent relationships.

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 would

Have a drop down and a value of can be manually

Entered.

return false;

}





public override StandardValuesCollection GetStandardValues (

ITypeDescriptorContext context)

{

return defaultrelations;

}

}

But how do you make a more customized UI? We can use the UITypeEditor class. The UITypeEditor class includes methods that can be invoked by the property window when displaying properties or editing properties such as Drop-down lists and ellipsis buttons.

Some property types similar to Image,color,font.name have a small graphical representation to the left of the property value, which is implemented through the PaintValue method of overloaded UITypeEditor. When the property window is given a property value that defines the editor, it is supplied to the editor a rectangular box object (Rectangle) and a Paint object (Graphic), which are included in the PaintValue method's event parameter Paintvalueeventargs. For example, we have a grade class that needs a graphical representation. Here 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 graphical representation on the left.




Figure 5. Enter age





It is not difficult to achieve 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 Like

To do custom painting.

return true;

}





public override void PaintValue (Paintvalueeventargs pe)

{

Choose the right bitmap based on the value

string bmpname = null;

Grade g = (Grade) pe. Value;

if (G.value > 80)

{

Bmpname = "Best.bmp";

}

else if (G.value > 60)

{

Bmpname = "Ok.bmp";

}

Else

{

Bmpname = "Bad.bmp";

}





Draw that bitmap onto the surface provided.

Bitmap B = New Bitmap (typeof (Gradeeditor), bmpname);

Pe. Graphics.DrawImage (b, PE. Bounds);

B.dispose ();

}

}

As we mentioned above, UITypeEditor can implement the properties of the Drop-down Selection and Pop-up dialog box selection. The following example will include such a code. If you want to know further information, refer to the Uitypeeditor.geteditstyle and Uitypeeditor.editvalue methods and the IWindowsFormsEditorService interface.

-----------------------------------

<<<<<<<<<<<< to Be Continued >>>>>>>>>>>>



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.