Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Richtextbox color change dinamically

Richtextbox color change dinamically

Scheduled Pinned Locked Moved C#
data-structurestutorial
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gericooper
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • G gericooper

      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

      G Offline
      G Offline
      gericooper
      wrote on last edited by
      #2

      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?

      G 1 Reply Last reply
      0
      • G gericooper

        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?

        G Offline
        G Offline
        gericooper
        wrote on last edited by
        #3

        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);

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups