C # property attribute and modify and edit multi-line text

Source: Internet
Author: User

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 ();

}

 

Related Article

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.