How to Select Text from the Richtextbox and Then Color It

How to select text from the RichTextBox and then color it?

Here's some code you can build on in order to achieve the functionality you want.

private void ColourRrbText(RichTextBox rtb)
{
Regex regExp = new Regex("\b(For|Next|If|Then)\b");

foreach (Match match in regExp.Matches(rtb.Text))
{
rtb.Select(match.Index, match.Length);
rtb.SelectionColor = Color.Blue;
}
}

The CodeProject article Enabling syntax highlighting in a RichTextBox shows how to use RegEx in a RichTextBox to perform syntax highlighting. Specifically, look at the SyntaxRichtTextBox.cs for the implementation.

Selecting text and changing it's color on a line of a RichTextBox

I hope I've understood you correctly.

This loops over each line in the richtextbox, works out which lines are the assembly comments, then makes everything red after the ";"

With FOREACH loop as requested

To use a foreach loop you simply need to keep track of the index manually like so:

// Index
int index = 0;

// Loop over each line
foreach (string line in richTextBox1.Lines)
{
// Ignore the non-assembly lines
if (line.Substring(0, 2) != ";;")
{
// Start position
int start = (richTextBox1.GetFirstCharIndexFromLine(index) + line.LastIndexOf(";") + 1);

// Length
int length = line.Substring(line.LastIndexOf(";"), (line.Length - (line.LastIndexOf(";")))).Length;

// Make the selection
richTextBox1.SelectionStart = start;
richTextBox1.SelectionLength = length;

// Change the colour
richTextBox1.SelectionColor = Color.Red;
}

// Increase index
index++;
}

With FOR loop

// Loop over each line
for(int i = 0; i < richTextBox1.Lines.Count(); i++)
{
// Current line text
string currentLine = richTextBox1.Lines[i];

// Ignore the non-assembly lines
if (currentLine.Substring(0, 2) != ";;")
{
// Start position
int start = (richTextBox1.GetFirstCharIndexFromLine(i) + currentLine.LastIndexOf(";") + 1);

// Length
int length = currentLine.Substring(currentLine.LastIndexOf(";"), (currentLine.Length - (currentLine.LastIndexOf(";")))).Length;

// Make the selection
richTextBox1.SelectionStart = start;
richTextBox1.SelectionLength = length;

// Change the colour
richTextBox1.SelectionColor = Color.Red;
}
}

Example of red highlighted richtext

Edit:

Re-reading your question I'm confused as to whether you wanted to make the ; red as well.

If you do remove the +1 from this line:

int start = (richTextBox1.GetFirstCharIndexFromLine(i) + currentLine.LastIndexOf(";") + 1);

How to change color for specific string in richTextBox

inside richTextBox_TextChanged check the full line of text when comparing

string str = "red string";
for(int i=0; i<richTextBox1.Lines.Length; i++)
{
string text = richTextBox1.Lines[i];
richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(i), text.Length);
if(text ==str)
{
richTextBox1.SelectionColor = Color.Red;
}else
{
richTextBox1.SelectionColor = Color.Black;
}
}

for multiple colors, I would use dictionary

var dictionary = new Dictionary<string, System.Drawing.Color>();
dictionary.Add("red color", System.Drawing.Color.Red);
dictionary.Add("Blue color", System.Drawing.Color.Blue);
//as above example you can use for loop and get each line of rich textbox
string linefromTextBox = "Blue color";
//then check that line contain of of text in the dictionaly
if (dictionary.ContainsKey(linefromTextBox))
{
// if key found then you can get the color as below
// asign this as SelectionColor
//before that you need to Select the line from rich text box as above example
var color = dictionary[linefromTextBox];
}

Change current color in a richTextBox?

Basically richTxtBox.FrontColor = Color.Red; will change the color of all text to Red, but you requirement is to change only the color of selected text, for that you need to use, richTextBox1.SelectionColor = Color.Red; which will change the color of only selected text.

You can decide which portion of the text need to be selected by using SelectionStart and SelectionLength. it is purely working on the basics of the index.

The mentioned problem of selecting the typed text in richTextBox1_TextChanged can be avoid by using this snippet

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionStart != 0)
{
int length = richTextBox1.Text.Length;
richTextBox1.Select(richTextBox1.SelectionStart - 1, 1);
richTextBox1.SelectionColor = Color.Red ;
richTextBox1.SelectionStart = length;
}
}

Apart from other answers here i have a magic box, which will changes the colors based on text.

Sample Image

And this can be achieved by using the following code:

private void button1_Click(object sender, EventArgs e)
{
int selectionStart = 0;
foreach (var item in richTextBox1.Text.Split(' '))
{
Color rgb = Color.FromName(item);
richTextBox1.SelectionStart = selectionStart;
richTextBox1.SelectionLength = item.Length;
richTextBox1.SelectionColor = rgb;
selectionStart += item.Length + 1;
}

}

Rich Text Box how to highlight text block

I think you are looking for ScintillaNET.

On the other hand if you want to do this by yourself in RTB then you can do it by first finding the lineNumber using TextBoxBase.Lines property. Then ...

//Select the line from it's number
startIndex = richTextBox.GetFirstCharIndexFromLine(lineNumber);
richTextBox.Select(startIndex, length);

//Set the selected text fore and background color
richTextBox.SelectionColor = System.Drawing.Color.White;
richTextBox.SelectionBackColor= System.Drawing.Color.Blue;

Color of selected text in RichTextBox

In WPF you can accomplish this as follows:

myRichTextBox.SelectionBrush = System.Windows.Media.Brushes.Yellow; // WPF
myRichTextBox.IsInactiveSelectionHighlightEnabled = true;

Unfortunately, the desired behavior is not possible in Windows Forms (details here). The workaround would be to use a WPF RichTextBox in the Windows Form through ElementHost.

References:

TextBoxBase.SelectionBrush Property (WPF)

TextBoxBase.IsInactiveSelectionHighlightEnabled Property (WPF)




EDIT:
Removed the WinForms solution, because SelectionBackColor does not provide the desired behavior.

Color specific words in RichtextBox

Add an event to your rich box text changed,

  private void Rchtxt_TextChanged(object sender, EventArgs e)
{
this.CheckKeyword("while", Color.Purple, 0);
this.CheckKeyword("if", Color.Green, 0);
}

private void CheckKeyword(string word, Color color, int startIndex)
{
if (this.Rchtxt.Text.Contains(word))
{
int index = -1;
int selectStart = this.Rchtxt.SelectionStart;

while ((index = this.Rchtxt.Text.IndexOf(word, (index + 1))) != -1)
{
this.Rchtxt.Select((index + startIndex), word.Length);
this.Rchtxt.SelectionColor = color;
this.Rchtxt.Select(selectStart, 0);
this.Rchtxt.SelectionColor = Color.Black;
}
}
}


Related Topics



Leave a reply



Submit