Two: UI attribute editor (UITOR)
The attribute editor here means that the form of the pop-up dialog and drop down UI mentioned above. Don't mention let us introduce below.
1, in the form of pop-up dialog
In this example I use the properties of the string type to display the version of the information, you can write a variety of properties, here you only need to specify the editor of the change attribute.
First we have to build a String type property, the code is as follows:
Private string _appver = "1.0";
[CategoryAttribute ("Custom Editor"),
DefaultValueAttribute ("1.0"),
DESCRIPTIONATTRIBUTE ("Version Information"),
ReadonlyAttribute (TRUE),
EditoAttribute (TypeOf (AppverConverter), TypeOf (System.drawing.Design.uityPeEditor)]
Public String Appver
{
Get {return this._appver;}
Set {this._appver = value;
}
Everyone may have noticed that there is a nature of this property editoattribute (typeof (appverconverter), typeof (system.drawing.design.uitypeeditor), the specific meaning can be referred to MSDN, I don't have to say more here, then Let's see how the appverConverter is achievable. The specific code is as follows:
///
/// Customize the attribute editor of the UI (pop up message)
/// summary>
Public class appverconverter: system.drawing.design.uitypeEditor
{
///
/// Overwrite this method to return to the type of editor.
/// summary>
Public override system.drawing.design.uitypeEditOtherleditstyle getEditStyle (System.componentmodel.ityped "criptorContext Context)
{
Return system.drawing.design.uitypeeditorEditStyle.Modal;
}
///
// / Overwrite this method to display version information
/// summary>
Public override Object EditValue (System.comPonentModel.ityped, System.iserviceProvider Provider, Object Value)
{
System.Windows.Forms.MessageBox.Show ( "Version: 1.0 / n Author: Zhang Xiang", "Version");
Return Value;
}
}
Here, we need to inherit our property editor must inherit from System.drawing.Design.uityPeEditor, or you can't display UI. The return value of the UITYPEEDITOREDITSTYLE method determines the type of change attribute editor. You can refer to MSDN I have said not much here. After compiling, you can see the following screen:
2, the type of drop down UI
The drop-down UI type is mainly to provide a simple interface to the user to select the properties you want to determine, this way is provided to the user very friendly interface. The following example we first define properties of a Point type, in the default, this type of properties are in the form of expansion to allow users to edit. Here we extend his feature, not only by direct input, but also pull out a control, the user can determine the specific value according to the position of the mouse on this control. The specific code below: private system.drawing.point_dropui;
[CategoryAttribute ("Custom Editor"),
DefaultValueAttribute ("1"),
DescriptionAttribute ("drop-down visual control"),
ReadonlyAttribute (FALSE),
EditoAttribute (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.uitypeEditOtherleditstyle getEditStyle (System.componentmodel.ityped "criptorContext Context)
{
Return system.drawing.design.uitypeeditorEditStyle.dropdown;
}
Public override Object EditValue (System.comPonentModel.ityped, 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 uiicontrol = 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.toTString ();
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),
THISHEIGHT- (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 is very simple, I believe that everyone can understand, if you don't understand, you explain it very clearly.
We need to overwrite the editvalue in the Property Editor, do you notice this parameter? In fact, this parameter is the attribute value already packaged. When we customize this value, you can also return a box value to determine the modified properties. In the two examples above, we just use this value, you can make a more personal editor after you understand this content. end:
There are still many Attributes in .NET Framework, because the younger brother knows limited, so you can only introduce these, and during this period, you can avoid mistakes. I hope everyone can forgive. The younger brother only hopes that I can get a role of a throwing brick, I can satisfy, the younger brother is very grateful to you can read these things I wrote. thank you all.
I hope everyone will contact:
E-mail:
Kilxy@hotmail.com
QQ: 20954664