Richtextbox color change dinamically
-
hi task: there's a string array with 1 & 2 char strings. If user strikes the chars matches among one in the array, the prog changes user input for example from "aa"(=user input) to "aA"(in array). It is all done handling the textchanged event of the richtextbox. (last 1 or last 2 chars input) but: the "one char string" would be nice to be presented by blue color the "2 char string" for example with red. Still cannot figure out why the code below doesn't work but it changes all chars red, it only correct for the first & second recognized thing like so: "aA"s" but when input again "aA" the whole thing is changed to red (but "s" should be remained blue) thx for your ideas the event handling
private void richTextBox1_TextChanged(object sender, EventArgs e) { if (richTextBox1.Text.Length >= 2 && !boolfound) { Array.Find(mnames, FindM); } }
invokes this(1 & 2 chars matching section):private bool FindM(String s) { if (s.Equals(richTextBox1.Text.Substring(richTextBox1.Text.Length - 2, 2), StringComparison.CurrentCultureIgnoreCase)) { richTextBox1.Text= richTextBox1.Text.Remove(richTextBox1.Text.Length - 2,2); richTextBox1.Focus(); richTextBox1.Select(richTextBox1.Text.Length, 0); richTextBox1.AppendText(s); richTextBox1.SelectionStart = richTextBox1.Text.Length - 2; richTextBox1.SelectionLength = 2; richTextBox1.SelectionColor = Color.Red; richTextBox1.DeselectAll(); richTextBox1.Select(richTextBox1.Text.Length, 0); boolfound = false; return true; } if(s.Equals(richTextBox1.Text.Substring(richTextBox1.Text.Length - 1, 1), StringComparison.CurrentCultureIgnoreCase)) { richTextBox1.Text=richTextBox1.Text.Remove(richTextBox1.Text.Length - 1,1); richTextBox1.Focus(); richTextBox1.Select(richTextBox1.Text.Length, 0); richTextBox1.AppendText(s); richTextBox1.SelectionStart = richTextBox1.Text.Length - 1; richTextBox1.SelectionLength = 1; richTextBox
-
hi task: there's a string array with 1 & 2 char strings. If user strikes the chars matches among one in the array, the prog changes user input for example from "aa"(=user input) to "aA"(in array). It is all done handling the textchanged event of the richtextbox. (last 1 or last 2 chars input) but: the "one char string" would be nice to be presented by blue color the "2 char string" for example with red. Still cannot figure out why the code below doesn't work but it changes all chars red, it only correct for the first & second recognized thing like so: "aA"s" but when input again "aA" the whole thing is changed to red (but "s" should be remained blue) thx for your ideas the event handling
private void richTextBox1_TextChanged(object sender, EventArgs e) { if (richTextBox1.Text.Length >= 2 && !boolfound) { Array.Find(mnames, FindM); } }
invokes this(1 & 2 chars matching section):private bool FindM(String s) { if (s.Equals(richTextBox1.Text.Substring(richTextBox1.Text.Length - 2, 2), StringComparison.CurrentCultureIgnoreCase)) { richTextBox1.Text= richTextBox1.Text.Remove(richTextBox1.Text.Length - 2,2); richTextBox1.Focus(); richTextBox1.Select(richTextBox1.Text.Length, 0); richTextBox1.AppendText(s); richTextBox1.SelectionStart = richTextBox1.Text.Length - 2; richTextBox1.SelectionLength = 2; richTextBox1.SelectionColor = Color.Red; richTextBox1.DeselectAll(); richTextBox1.Select(richTextBox1.Text.Length, 0); boolfound = false; return true; } if(s.Equals(richTextBox1.Text.Substring(richTextBox1.Text.Length - 1, 1), StringComparison.CurrentCultureIgnoreCase)) { richTextBox1.Text=richTextBox1.Text.Remove(richTextBox1.Text.Length - 1,1); richTextBox1.Focus(); richTextBox1.Select(richTextBox1.Text.Length, 0); richTextBox1.AppendText(s); richTextBox1.SelectionStart = richTextBox1.Text.Length - 1; richTextBox1.SelectionLength = 1; richTextBox
Up! also, There may be a way which work (1st: getting the proper string from the array 2nd: coloring all matching string in the textbox) but I guess this would be time & resource consuming. any idea?
-
Up! also, There may be a way which work (1st: getting the proper string from the array 2nd: coloring all matching string in the textbox) but I guess this would be time & resource consuming. any idea?
found the thing: for all who is interested: the Remove method caused the whole thing to be colored out with the same color the right code is:
richTextBox1.Focus(); richTextBox1.Select(richTextBox1.Text.Length, 0); //richTextBox1.AppendText(s); richTextBox1.SelectionStart = richTextBox1.Text.Length - 2; richTextBox1.SelectionLength = 2; richTextBox1.SelectionColor = Color.Red; richTextBox1.SelectedText = s; richTextBox1.DeselectAll(); richTextBox1.Select(richTextBox1.Text.Length, 0);