The usage method of RichTextBox in C# is basically the same as TextBox, except that RichText also supports RTF format documents in addition to TXT. This article introduces the usage method of RichTextBox in detail for your reference, as follows:
1. How to use RichTextBox
method
The RichTextBox control not only allows text input and editing, but also provides many features in more advanced specified formats that are not available in standard TextBox controls.
Syntax: RichTextBox
illustrate:
RichTextBox provides properties that can be specified for any part of this control text. To change the format of the text, first select it. Only selected text can be assigned to character and paragraph formats. Using these properties, you can change the text to bold or italics, or change its color, and create superscripts and subscripts. The format of the paragraph can be adjusted by setting left and right indentation and hanging indentation.
The RichTextBox control can open and save files in two forms: rtf format and ordinary ASCII text format. You can use the control's methods (LoadFile and SaveFile) to read and write files directly, or use control properties such as SelRTF and TextRTF associated with Visual Basic file input/output statements.
By using the OLEObjects collection, the RichTextBox control supports embedding of objects. Each object inserted into the control represents an OLEObject object. Using such a control, you can create documents containing other documents or objects. For example, you can create a document that has an embedded Microsoft Excel spreadsheet, or Microsoft Word document, or other OLE object that has been registered in the system. To insert an object into a RichTextBox control, simply drag a file (such as drag in Windows 95 Explorer), or drag a highlighted area of the file used by another application (such as Microsoft Word), and then drop the dragged content directly into the control.
The RichTextBox control supports clipboard and OLE drag/drop operations for OLE objects. When an object is pasted from the clipboard, it is inserted at the current insertion point. When an object is dragged and dropped onto a control, the insertion point will track the movement of the mouse cursor until the object is inserted when the mouse button is released. This behavior is the same as Microsoft Word.
Use the SelPrint method to print all or part of the text of the RichTextBox control.
Because RichTextBox is a data binding control, it can be bound to the Binary or Memo fields of a Microsoft Access database, or to other database fields with the same capacity (for example, fields with TEXT data type in SQL servers).
The RichTextBox control can support almost all properties, events, and methods used by the standard TextBox control, such as MaxLength, MultiLine, ScrollBars, SelLength, SelStart, and SelText. For applications where TextBox controls can be used, the RichTextBox controls can also be easily used. Moreover, the RichTextBox control does not have the same limit of 64K character capacity as the standard TextBox controls.
Release Notes In order to use the RichTextBox control in the application, the file must be added to the project. Therefore, when the application is released, the files should be installed in the SYSTEM directory of Microsoft Windows.
2. RichTextBox instance code:
private void Open the graphics fileToolStripMenuItem_Click(object sender, EventArgs e) { string NameFile; if (this.() == ) { NameFile = this.; if (NameFile != "") { this. = (NameFile); } } } private void Open a text fileToolStripMenuItem_Click(object sender, EventArgs e) { string Filename; = false; if (this.() == ) { Filename = ; if (Filename != "") { this. = Filename; this.(@Filename, ); } } } //Constructor this. += new KeyPressEventHandler(textBox1_KeyPress); this. += new CancelEventHandler(textBox1_Validating); this. += new LinkClickedEventHandler(richTextBox1_LinkClicked); //Cancel or set to bold private void button2_Click(object sender, e) { Font oldFont = this.; Font newFont; if () newFont = new Font(oldFont, & ~); else newFont = new Font(oldFont, | ); this. = newFont; this.(); } //Cancel or set to italic private void button7_Click(object sender, e) { Font oldFont = this.; Font newFont; if () newFont = new Font(oldFont, & ~); else newFont = new Font(oldFont, | ); this. = newFont; this.(); } //Cancel or underline private void button8_Click(object sender, e) { Font oldFont = this.; Font newFont; if () newFont = new Font(oldFont, & ~); else newFont = new Font(oldFont, | ); this. = newFont; this.richTextBox1 .Focus(); } //Cancel or center private void button5_Click(object sender, e) { if (this. == ) this. = ; else this. = ; this.(); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if(( < 48 || > 57) && != 8 && !=13) { = true; } else if( == 13) { TextBox txt = (TextBox)sender; if( > 0) ApplyTextSize(); = true; this.(); } } private void textBox1_Validating(object sender, CancelEventArgs e) { TextBox txt = (TextBox)sender; ApplyTextSize(); this.(); } //Change the font size private void ApplyTextSize(string textSize) { float newSize = (textSize); FontFamily currentFontFamily; Font newFont; currentFontFamily = this.; newFont = new Font(currentFontFamily, newSize); this. = newFont; } //Open the web page private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e) { (); } //Open the file private void button1_Click(object sender, e) { try { this.(@"..\..\"); } catch() { ("File not found!"); } } //Save the file private void button6_Click(object sender, e) { try { this.(@"..\..\"); } catch( err) { (); } }
3. Search text within the content of RichTextBox:
1. Overload list:
Search the first instance of a character in the character list in the text of the RichTextBox control
public int Find(char[]);
The following example searches the content of a RichTextBox for characters passed to the method in the text parameter. If the contents of the text array are found in the RichTextBox, the method returns the index of the found value; otherwise, it returns -1. This example assumes that this method is in a class in Form, which contains a RichTextBox control named richTextBox1 and a Button control (named button1) connected to the click event handling method defined in this example.
The following code:
private void button1_Click(object sender, e) { (FindMyText(new char[]{'D','e','l','t','a'}).ToString()); } public int FindMyText(char[] text) { // Initialize the return value to false by default. int returnValue = -1; // Ensure that a search string has been specified and a valid start point. if ( > 0) { // Obtain the location of the first character found in the control // that matches any of the characters in the char array. int indexToText = (text); // Determine whether the text was found in richTextBox1. if(indexToText >= 0) { // Return the location of the character. returnValue = indexToText; } } return returnValue; }
2. Search for strings in the text of the RichTextBox control.
public int Find(string);
Starting from a specific starting point, search the text of the RichTextBox control for the first instance of a character in the character list.
public int Find(char[], int);
Search for strings in the text of the RichTextBox control when applying specific options to the search.
public int Find(string, RichTextBoxFinds);
The following example searches the entire content of the RichTextBox for the first instance of the search string passed into the text parameter of this method. If the search string is found in the RichTextBox, this method returns a true value and highlights the text; otherwise, it returns false. This example also specifies options in the search that matches the case of the specified search string. This example assumes that this method is placed in a class in Form and that the class contains a RichTextBox named richTextBox1.
The specific code is as follows:
public bool FindMyText(string text) { // Initialize the return value to false by default. bool returnValue = false; // Ensure a search string has been specified. if ( > 0) { // Obtain the location of the search string in richTextBox1. int indexToText = (text, ); // Determine if the text was found in richTextBox1. if(indexToText >= 0) { returnValue = true; } } return returnValue; }
Search for the first instance of a character in a text range of the RichTextBox control.
public int Find(char[], int, int);
In case of applying specific options to search, search the text of the RichTextBox control for strings that are located at a specific location within the control.
public int Find(string, int, RichTextBoxFinds);
Search for strings within a text range within the control in the RichTextBox control text when applying specific options to search.