I need to create a winform to change the color of expressions that start with '@'.
For example:
nothing @examples nadamais
The @expressao must be of another color.
The text will come from the input of a textbox that the user types (can be a richTextBox or something from Infragistics, whatever, as long as it works).
I already tried to separate the string from the textbox with split and detect which of them starts with @. But after that, I could not color the letters and send them back to the textbox.
I also tried something with the selectionColor of the richTextBox, however the selection would clear the @ and the cursor would go to the left of the inserted character, besides I could not "stop" the selection when I wanted.
I'm taking a look at Regex to see if I can apply to my problem.
I'm using C # in Visual Studio Community 2017.
EDIT
I changed the code that is in the comments to try to make it work in my case, but the limiter does not work. I believe this happens because the selection starts only from an '@', but there may be a '' before, hence buga.
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
int current = richTextBox1.SelectionStart;
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
string line = richTextBox1.Lines[i];
int index = line.IndexOf(' '), lineFirstIndex = line.IndexOf('@');
if (index != -1 && lineFirstIndex != -1)
{
richTextBox1.Select(lineFirstIndex, index);
richTextBox1.SelectionColor = Color.Red;
}
else
{
lineFirstIndex = richTextBox1.GetFirstCharIndexFromLine(i);
richTextBox1.Select(lineFirstIndex, line.Length);
richTextBox1.SelectionColor = Color.Empty;
}
}
richTextBox1.Select(current, 0);
}