Description of attributes (attribute) of the custom component (d)

Source: Internet
Author: User
Tags tostring uicontrol
Two: UI Property Editor (UITypeEditor)

The property editor here means that you can implement the pop-up dialog box and the dropdown UI as mentioned above. Nonsense do not say the following we introduce.

1, the form of pop-up dialog box

In this example, I use the string type of properties to display the version of the information, you can casually write various types of properties, here only need to specify the editor of the property can be changed.

First we're going to create a string type of property with the following code:

private string _appver= "1.0";



[CategoryAttribute ("Custom editor"),

DefaultValueAttribute ("1.0"),

DescriptionAttribute ("Version information"),

ReadOnlyAttribute (True),

EditorAttribute (typeof (Appverconverter), typeof (System.Drawing.Design.UITypeEditor))]

public string AppVer

{

get {return this._appver;}

set {This._appver=value;}

}

You may have noticed a property in this attribute EditorAttribute (typeof (Appverconverter), typeof (System.Drawing.Design.UITypeEditor)), Everyone can refer to MSDN I don't have to say much here, so let's see how appverconverter this class is going to be. The specific code is as follows:

<summary>

Custom UI Property Editor (pop-up message)

</summary>

public class AppVerConverter:System.Drawing.Design.UITypeEditor

{

<summary>

Override this method to return the type of the editor.

</summary>

public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle ( System.ComponentModel.ITypeDescriptorContext context)

{

return System.Drawing.Design.UITypeEditorEditStyle.Modal;

}



<summary>

Override this method to display version information

</summary>

public override Object EditValue (System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider Provider, object value)

{

System.Windows.Forms.MessageBox.Show ("version: 1.0\n Author: Zhang", "version information");

return value;

}

}

The explanation here is that our property editor must inherit from System.Drawing.Design.UITypeEditor, or else we won't be able to display the UI. The return value of the Uitypeeditoreditstyle method determines the type of the Modifier property editor you can refer to MSDN I'm not going to say much here. After compiling, you can see the following picture:




2. Type of dropdown UI

The dropdown UI type is primarily provided to the user with a simple interface to select the attributes to be determined, a way that provides a very user-friendly interface to the user. For the following example, we first define the attribute of a point type in which, by default, this type of property is expanded to allow the user to edit it. Here we extend his function not only to change the value by direct input, but also to pull out a control that allows the user to determine the exact value based on the mouse position on the control. The following specific code:

Private System.Drawing.Point _dropui;



[CategoryAttribute ("Custom editor"),

DefaultValueAttribute ("1"),

DescriptionAttribute ("Drop-down visual Control"),

ReadOnlyAttribute (False),

EditorAttribute (typeof (Dropeditor), typeof (System.Drawing.Design.UITypeEditor))]

Public System.Drawing.Point Dropui

{

get {return this._dropui;}

set {This._dropui=value;}

}







public class DropEditor:System.Drawing.Design.UITypeEditor

{



public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle ( System.ComponentModel.ITypeDescriptorContext context)

{

return System.Drawing.Design.UITypeEditorEditStyle.DropDown;

}



public override Object EditValue (System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider Provider, object value)

{

System.Windows.Forms.Design.IWindowsFormsEditorService iws= ( System.Windows.Forms.Design.IWindowsFormsEditorService) provider. GetService (typeof (System.Windows.Forms.Design.IWindowsFormsEditorService));

if (iws!=null)

{

Propertygridapp.dropuicontrol uicontrol=new Propertygridapp.dropuicontrol ((System.Drawing.Point) VALUE,IWS);

IWs. Dropdowncontrol (Uicontrol);

return uicontrol.value;

}

return value;

}

}



Internal class DropUIControl:System.Windows.Forms.UserControl

{

Public Dropuicontrol (System.Drawing.Point avalue,system.windows.forms.design.iwindowsformseditorservice IWS)

{

This. Value=avalue;

This._tmpvalue=avalue;

THIS._IWS=IWS;

This. SetStyle (system.windows.forms.controlstyles.doublebuffer| System.windows.forms.controlstyles.userpaint| System.windows.forms.controlstyles.allpaintinginwmpaint,true);

This. Backcolor=system.drawing.systemcolors.control;

}



Private System.Drawing.Point _value;

Public System.Drawing.Point Value

{

get {return this._value;}

set {This._value=value;}

}



Private System.Drawing.Point _tmpvalue;

Private System.Windows.Forms.Design.IWindowsFormsEditorService _IWS;



protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)

{

String str= "X:" +this._tmpvalue. X.tostring () + "; Y: "+this._tmpvalue." Y.tostring ();

System.Drawing.Graphics G=e.graphics;

System.Drawing.SizeF sizef= g.measurestring (str,this. Font);

g.DrawString (str,

This. Font,

New System.Drawing.SolidBrush (System.Drawing.Color.Black),

(int) (This. width-(int) SizeF. Width)/2),

This. height-(int) SizeF. Height);

G.pageunit=system.drawing.graphicsunit.pixel;

G.fillellipse (New System.Drawing.SolidBrush (System.Drawing.Color.Red),

This. Value.x-2,

This. Value.y-2,

4,

4);

}



protected override void OnMouseMove (System.Windows.Forms.MouseEventArgs e)

{

Base. OnMouseMove (e);

This._tmpvalue=new System.Drawing.Point (E.X,E.Y);

This. Invalidate ();

}



protected override void OnMouseUp (System.Windows.Forms.MouseEventArgs e)

{

Base. OnMouseUp (e);

This. Value=this._tmpvalue;

This. Invalidate ();

if (e.button==system.windows.forms.mousebuttons.left)

This._iws. Closedropdown ();



}

}




The above code degree is very simple, I believe you will be able to understand, if there is not understand the place to see help, it explains very clearly.

In writing the property editor we all need to overwrite the EditValue method, do you notice the parameter of object value? This parameter is actually a boxed attribute value, and we can also return a boxed value to determine which property has been modified when we have finished processing this value. In the above two examples we simply use this value, you understand this content will be able to make a more personalized editor.


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.