9 Replies - 9382 Views - Last Post: 18 June 2011 - 06:49 AM Rate Topic: -----

#1 EpicTTR  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 15-June 11

How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 04:15 PM

In Visual C#.NET, I want to change the color of specific text when it is typed in to a rich text box.

For example, I might want to color the world "hi", so when the user is typing in the rich text box, if they type "hi" it would immediately change colors and the word "hi" would be a different color from all the other text in the rich text box.

How would I do that?

I've tried something like:
            richTextBox.Select(rtxtMain.Text.IndexOf("hi"), "hi".Length);
            richtextBox.SelectionColor = Color.Aqua;

Its error/warning free, but when I debug it and type in "hi" the program just crashes.

So, how would I change the color of specific words (but not that of other words) while a user is typing into a rich text box?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I color a specific word in a Rich Text Box in C#?

#2 EpicTTR  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 15-June 11

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 05:54 PM

Bump, please help me.
Was This Post Helpful? 0
  • +
  • -

#3 EpicTTR  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 15-June 11

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 10:05 PM

    rtxtMain.Select(rtxtMain.Text.IndexOf("hi"), "hi".Length);
    rtxtMain.SelectionColor = Color.Aqua;
    rtxtMain.SelectionFont = new Font("Times New Roman", FontStyle.Bold);


That was the code I was told to use, however, I have some problems with it.

By the way. "rtxtMain" is the rich textbox.
Anyways, its error/warning free until I run my program, as soon as I try to type in the rich text box, the program crashes, highlighting this line of code:

    rtxtMain.Select(rtxtMain.Text.IndexOf("hi"), "hi".Length);


And saying:

Quote

InvalidArgument=Value of '-1' is not valid for 'start'.
Parameter name: start


So, what exactly is wrong, and how do I fix it?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 10:20 PM

You have to check the index before you can stick it into the select. What it is saying is that when you type it does a search for "hi" and if it doesn't find it (which it won't after you type just "h") it then returns -1 and that is not valid.

if (rtxtMain.Text.IndexOf("hi") > -1) {
   // Now do the select and color change
}



Make sure that the word is actually found in the text first before you attempt to select it and change its color. Makes sense right? If it is not found, you don't want to attempt to do a select. :)
Was This Post Helpful? 0
  • +
  • -

#5 EpicTTR  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 15-June 11

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 10:35 PM

View PostMartyr2, on 17 June 2011 - 10:20 PM, said:

You have to check the index before you can stick it into the select. What it is saying is that when you type it does a search for "hi" and if it doesn't find it (which it won't after you type just "h") it then returns -1 and that is not valid.

if (rtxtMain.Text.IndexOf("hi") > -1) {
   // Now do the select and color change
}



Make sure that the word is actually found in the text first before you attempt to select it and change its color. Makes sense right? If it is not found, you don't want to attempt to do a select. :)


Yeah, but now when I do that:

            if (rtxtMain.Text.IndexOf("hi") > -1)
            {
                rtxtMain.Select(rtxtMain.Text.IndexOf("hi"), "hi".Length);
                rtxtMain.SelectionColor = Color.BlueViolet


It just makes my typing sorta go crazy (cursor jumps around) and in the end it just deletes the word instead of coloring it...

How do I fix it?

View PostMartyr2, on 17 June 2011 - 10:20 PM, said:

You have to check the index before you can stick it into the select. What it is saying is that when you type it does a search for "hi" and if it doesn't find it (which it won't after you type just "h") it then returns -1 and that is not valid.

if (rtxtMain.Text.IndexOf("hi") > -1) {
   // Now do the select and color change
}



Make sure that the word is actually found in the text first before you attempt to select it and change its color. Makes sense right? If it is not found, you don't want to attempt to do a select. :)


Yeah, but now when I do that:

            if (rtxtMain.Text.IndexOf("hi") > -1)
            {
                rtxtMain.Select(rtxtMain.Text.IndexOf("hi"), "hi".Length);
                rtxtMain.SelectionColor = Color.BlueViolet


It just makes my typing sorta go crazy (cursor jumps around) and in the end it just deletes the word instead of coloring it...

How do I fix it?
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 10:43 PM

What event are you putting this into? A keypress or change event or something else? If you put it in a change event then the fact you are selecting and altering text through the program may kick off other change events and lead to crazy stuff.

:)
Was This Post Helpful? 0
  • +
  • -

#7 EpicTTR  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 15-June 11

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 10:45 PM

View PostMartyr2, on 17 June 2011 - 10:43 PM, said:

What event are you putting this into? A keypress or change event or something else? If you put it in a change event then the fact you are selecting and altering text through the program may kick off other change events and lead to crazy stuff.

:)


Well, my goal is to have it change as soon as the user types it in, so I put it in the change event for the box. Is there a better way to do it?
Was This Post Helpful? 0
  • +
  • -

#8 EpicTTR  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 15-June 11

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 10:53 PM

View PostMartyr2, on 17 June 2011 - 10:43 PM, said:

What event are you putting this into? A keypress or change event or something else? If you put it in a change event then the fact you are selecting and altering text through the program may kick off other change events and lead to crazy stuff.

:)

I think the problem is, that I never deselect the word, so it replaces the word with a space (once I type in the space). So how do I deselect the word before that happens?
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,693
  • Joined: 08-June 10

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 17 June 2011 - 10:57 PM

Duplicate posts merged. Please do not open multiple threads for the same topics.
Was This Post Helpful? 0
  • +
  • -

#10 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: How do I color a specific word in a Rich Text Box in C#?

Posted 18 June 2011 - 06:49 AM

I've had this problem before, with colors and italics. Maybe there is an issue with the iteration through the rtBox in the search for "hi". I'm trying to figure this out myself. Will revisit. >_<

As for having these actions done while typing, a keyPress(which I believe someone here mentioned already) event with these statements should be sufficient. You could also use a timer.

Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1