To make specific text bold in a RichTextBox control in C#, you can use the SelectionFont
property along with the FontStyle
enumeration. Here’s an example of how you can achieve that:
private void MakeTextBold(string searchText)
{
int startIndex = 0;
while (startIndex < richTextBox1.TextLength)
{
int index = richTextBox1.Find(searchText, startIndex, RichTextBoxFinds.None);
if (index != -1)
{
richTextBox1.Select(index, searchText.Length);
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
startIndex = index + searchText.Length;
}
else
{
break;
}
}
}
In this example, the MakeTextBold
method takes a searchText
parameter, which represents the specific text you want to make bold. The method searches for the searchText
within the RichTextBox
control using the Find
method.
If a match is found, the method selects the text using the Select
method and sets the SelectionFont
property to a new font with the FontStyle.Bold
option.
The startIndex
variable keeps track of the position from where the next search should begin. The loop continues until no more matches are found.
To use this method, you can call it and provide the specific text you want to make bold. For example:
MakeTextBold("specific");
This will search for the text “specific” within the RichTextBox
control and make it bold.