. 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