Textbox and RichTextBox

Source: Internet
Author: User

. Net has two built-in controls to extract user input text :. Both controls are derived from the base class textboxbase, while textboxbase is derived from control.

Textboxbase provides basic functions to process text in a text box, such as selecting text, cutting and pasting from the clipboard, and many events.

Use the validating event of textbox to verify input of the same type.CodeCategory to reduce repeated code writing. For example, if both the name and address are input boxes and the verification rules are not empty, you can use the following code to subscribe to the corresponding verification event when initializing the form.

This.txt name. Validating + = new system.componentmodel.canceleventhandler(this.txt boxempty_validating );

This.txt address. Validating + = new system.componentmodel.canceleventhandler(this.txt boxempty_validating );

 

Private void txtboxempty_validating (Object sender, system. componentmodel. canceleventargs E)

{

Textbox TB;

TB = (textbox) sender;

// If the text is empty we set the background color of

// Textbox to red to indicate a problem. We use the tag Value

// Of the control to indicate if the control contains valid

// Information.

If (Tb. Text. Length = 0)

{

TB. backcolor = color. Red;

TB. Tag = false;

}

Else

{

TB. backcolor = system. Drawing. systemcolors. window;

TB. Tag = true;

}

}

 

RichTextBox allows you to format the text content, such as bold, italic, hyperlink, and center.

Example of reading text:

/// <Summary>

/// Open the file

/// </Summary>

/// <Param> </param>

/// <Param> </param>

Private void fbtnopenfile_click (Object sender, system. eventargs E)

{

Openfiledialog1.showdialog ();

Ftxtfilepath. Text = openfiledialog1.filename;

}

/// <Summary>

/// Read the file content

/// </Summary>

/// <Param> </param>

/// <Param> </param>

Private void fbtnreadcontent_click (Object sender, system. eventargs E)

{

Streamreader sr = new streamreader (ftxtfilepath. Text );

 

String input;

Do

{

Input = Sr. Readline ();

If (input! = "")

{

This. frtbfilecontent. Text + = input + "\ r \ n ";

}

} While (Sr. Peek ()! =-1 );

 

Sr. Close ();

}

/// <Summary>

/// Save the file content

/// </Summary>

/// <Param> </param>

/// <Param> </param>

Private void fbtnsavecontent_click (Object sender, system. eventargs E)

{

Streamwriter Sw = new streamwriter (this. ftxtfilepath. Text );

Sw. Write (frtbfilecontent. Text );

Sw. Flush ();

Sw. Close ();

}

The frtbfilecontent control is a RichTextBox Control.

Small Example of adding a RichTextBox on 2006-4-7
RichTextBox small example download

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.