Highlight the selected string or text in asp.net richTextBox

  • 2020-05-16 06:46:11
  • OfStack

Example verification is as follows:
 
private void  The highlight (string  To find a string ) 
{ 
// First find the starting position of the string you are looking for  
int  The starting position =richTextBox Phrases show .Find( To find a string ); 
// judge 1 Whether to find it or not , If you can't find it then the starting position is -1 
if ( The starting position >=0) 
{ 
richTextBox Phrases show .SelectionStart =  The starting position ; 
// Get the length of the string  
richTextBox Phrases show .SelectionLength =  To find a string .Length; 
// You can then change the color of the string  
richTextBox Phrases show .SelectionColor = Color.Red; 
} 
} 

Summary: to use a program to do some formatting on the selected text or string, you need to use richTextBox, not normal TextBox.
One of the most common is:
richTextBox.Find: to find a string and get its starting position
richTextBox.SelectionStart: gets or sets the starting position of the string to select
richTextBox.SelectionLength: gets or sets the length of the string to select
And then finally the formatting, in the example above we only changed one color,
You can also change the size, font and so on, depending on your needs.

For example, the phrase richTextBox is displayed.SelectionFont = new Font(" bold ", 13);

Related articles: