C # property attribute and modify and edit multi-line text
The use of a common attribute. The name attribute does not define private fields, while the age defines existing fields.
Public sealed class employee {
// This property is an automatically implemented property
Public string name {get; set ;}
Private int32 m_age;
Public int32 age {
Get {return (m_age );}
Set {
If (value <0) // the 'value' keyword always identifies the new value.
Throw new argumentoutofrangeexception ("value", value. tostring (),
"The value must be greater than or equal to 0 ");
M_age = value;
}
}
}
Change the editing style of the propertygrid Control (2) -- edit multiple lines of text
Effect:
Applicable scenarios:
1. edit multiple lines of text;
2. Edit long text.
Step 1: Define a class derived from uitypeeditor, for example:
Using system;
Using system. windows. forms;
Using system. drawing. design;
Using system. windows. forms. design;
Namespace blog.csdn.net. zhangyuk
{
/// <Summary>
/// Summary of propertygridmutitext.
/// </Summary>
Public class propertygridrichtext: uitypeeditor
{
Public override uitypeeditoreditstyle geteditstyle (system. componentmodel. itypedescriptorcontext context)
{
Return uitypeeditoreditstyle. dropdown;
}
Public override object editvalue (system. componentmodel. itypedescriptorcontext context, system. iserviceprovider provider, object value)
{
Try
{
Iwindowsformseditorservice edsvc =
(Iwindowsformseditorservice) provider. getservice (typeof (iwindowsformseditorservice ));
If (edsvc! = Null)
{
If (value is string)
{
Richtextbox box = new richtextbox ();
Box. text = value as string;
Edsvc. dropdowncontrol (box );
Return box. text;
}
}
}
Catch (exception ex)
{
System. console. writeline ("propertygridrichtext error:" + ex. message );
Return value;
}
Return value;
}
}
}
Step 2: edit the attribute class and specify the edit attribute. Example:
Namespace blog.csdn.net. zhangyuk
{
Public class someproperties
{
Private string _ finished_time = "";
//......
// Multi-line text edit box
String _ mutilinesample = "";
[Description ("multi-line text edit box"), category ("attribute"), editorattribute (typeof (propertygridrichtext ),
Typeof (system. drawing. design. uitypeeditor)]
Public string multi-line text
{
Get {return _ mutilinesample ;}
Set {_ mutilinesample = value ;}
}
//......
}
}
Step 3: Set the attribute object of propertygrid. Example:
Private void form1_load (object sender, system. eventargs e)
{
This. propertygrid1.selectedobject = new someproperties ();
}