Have you written controls today? --Asp.net control development series (6)

Source: Internet
Author: User
Uitypeedit "I want Peach"

Assume that you are now working on a "Poker" control. Playing cards have a property-color. After you select the color property, the property window will not only display text, there is also a small color icon to represent the color, "red peach" has a small "red peach" icon in front of the display, "Black peach" there is a "black peach" icon in front of the display, just like when you select the backcolor of other controls, there is a small color block before the color to represent the selected color, more considerate design ah.
Now, let's do this:

Public   Class Squeezer
{
.
 
 PublicCardtypes cardType
 {

}
}
[Editor ( Typeof (Cardtypeseditor ), Typeof (System. Drawing. Design. uitypeeditor)]
Public   Class Cardtypes
{
..
}

  Public   Class Cardtypeseditor: uitypeeditor
  {
Public   Override   Bool Getpaintvaluesupported (itypedescriptorcontext context)
{
Return True;//Support for creating small images
}

Public   Override   Void Paintvalue (paintvalueeventargs PE) // Define the logic for creating a thumbnail Based on the Value
{
String BMP name =   Null ;
Cardtypes C = (Cardtyes) PE. value;
Switch (C. Value)
{
CaseCardertypes. Hongtao:
BMP name= "Fig";//The image must be an embedded resource. The size is 16*16 and the image type is BMP.
Break;

}

Bitmap B =   New Bitmap ( Typeof (Gradeeditor), BMP name );

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

B. Dispose ();
}
}

AboveCodeIn, we use editorattribute to associate the color class with an editor, and then use this editor to draw a thumbnail.

Now, your creativity may encourage you to think about a new problem. I don't want users to simply use a simple drop-down list that only presents values (implemented through enumconverter) to select the attribute value, I want to implement very friendly interactions such as backcolor and dock for users to use. Well, let's proceed to the next step.
First, we need to make a suitable small window (cardtypeseditorcontrol) to customize the interface for interaction. This form inherits from system. window. forms. usercontrol or system. window. forms. in short, it is a winfrom form. I will not discuss how to do this form here, But you will aggregate it in this form class (not a combination, here it is a reference, by Editor) An iwindowsformseditorservice enables better interaction and controls when to close the following window opened, for example, edsvc in the up event of the mouse button. closedropdown (), so that the user can close the form and return the value after clicking and selecting the mouse.
Okay. Let's see how our editor is expanded:

Cardtypes target;
Cardtypeseditorcontrol UI;

Public Cardtypesuieditor (cardtypes target)
{
This. Target=Target;
}
// Logic Implementation of values that can be operated by users through the editor
Public   Override   Object Editvalue (itypedescriptorcontext context, iserviceprovider sp, Object Value)
{
// Get the IDE interaction Service
Iwindowsformseditorservice edsvc = (Iwindowsformseditorservice) sp. getservice ( Typeof (Iwindowsformseditorservice ));

If (Edsvc =   Null )
{
ReturnValue;
}

If (Ui =   Null )
{
UI= NewCardtypeseditorcontrol ();//Create an attribute operation form instance
}

Ui. selectedcardtypes = (Cardtypes) value; // The original value, selectedcardtypes attribute is just an example. You can implement your own logic at will.
Ui. editorservice = Edsvc; // Upload the service in the past. See the instructions above.
Ui. Target = (Cardtypes) Context. instance; // Get the instance of the connection object
Edsvc. dropdowncontrol (UI ); // Display the form as a downstream form. The optional values include edsvc. showdialog (UI ),
// In this case, a pop-up form is used to display the form. You may prefer this method.

Return Ui. selectedcardtypes; // In the form logic, we change the selectedcardtypes value and get this value when the form is closed.
}
// Style appears in the window. The optional values include modal. A ellipsis is displayed after the attribute. Click the pop-up mode form or a dialog box with a window, just like collectioneditor,
// None, no operation UI appears, honestly fill in the value
Public   Override System. Drawing. Design. uitypeeditoreditstyle geteditstyle (itypedescriptorcontext context)
{
ReturnSystem. Drawing. Design. uitypeeditoreditstyle. dropdown;
}

Oh, is there a way to play with you?
Return urlbuilder is used in the editvalue method. buildurl (icomponent) context. instance, null, (string) value, caption, filter, options) You can make a URL editor; Use coloreditor. colorui = new coloreditor. colorui (this); colorui. start (edsvc, value); edsvc. dropdowncontrol (colorui); in this case, a color selection interactive interface can be implemented. (Note that the Code does not handle exceptions such as null)

 

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.