C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-14-2008, 04:25 PM   #1
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,279
Change RichTextBox Color

I want to change specific text color in a RichTextBox. I want to insert something with a specific color. The solutions I find by googling require, if I understand correctly, that you read again all the text and then paint it accordingly. Can't you just insert something, color it and stay colored?

The methods I use is Select() to select a text and SelectionColor to change it. It seems simple but it doesn't really work for me.

Like, will this color blue str inserted to output rich text box?

Code:
output.Text += str;
output.Select(output.Text.Length - 1 - str.Length, str.Length);
output.SelectionColor = Color.Blue;
C_ntua is offline   Reply With Quote
Old 12-15-2008, 01:19 AM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
If output.Text is initially empty, and str is of length 3, you'll start selection at index 3 - 1- 3 = -1. That's not what you want, is it?
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 12-15-2008, 03:03 AM   #3
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,279
Probably not. Eh, it was just a quick example, i should emmit the -1.
What I get is color the text. But then whatever I insert to the richbox it also goes blue. I am close but can't seem to get it working.
C_ntua is offline   Reply With Quote
Old 12-15-2008, 03:10 AM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Haven't tested anything, but it sounds like a feature, not a bug, that appended text will use the font/style/color of the last character of the existing text.
As for a quick fix why not always set a color, black if no other is specified?
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 12-18-2008, 10:43 AM   #5
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,279
That was my main idea. But it didn't work properly. I ll try it more and see...
C_ntua is offline   Reply With Quote
Old 12-18-2008, 01:01 PM   #6
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,279
OK.
The RichTextBox has two properties.
Text and Rtf.
The text is the string it displays. The Rtf is the full string, which is the Text and some parameters about various things.
The thing is that when you do this:
Code:
r.Text = r.Text;
then every color that you have set will be lost. So I cannot do += to update the text.
It actually just takes the Text and wraps it with a header like "{options\blah\blah USEFUL TEXT \options\blah\blah}". I you select something and change the color you will get the right thing, but when you want again to insert text it will take the updated text and wrap around a header.

There has to be a standard way to add text with a certain color. Obviously doing:
Code:
r.Text += "somestring";
doesn't work as I want, but what else can I do? I could of course create my own method for doing so, but there has to be one already
C_ntua is offline   Reply With Quote
Old 12-18-2008, 03:22 PM   #7
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Code:
private void AppendColoredText(System.Windows.Forms.RichTextBox RichTextBox, string Text, System.Drawing.Color Color)
{
	var StartIndex = RichTextBox.TextLength;
	RichTextBox.AppendText(Text);
	var EndIndex = RichTextBox.TextLength;
	RichTextBox.Select(StartIndex, EndIndex - StartIndex);
	RichTextBox.SelectionColor = Color;
}
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 12-18-2008, 03:39 PM   #8
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,279
OK. I found a workaround. But there should be a better way to do this.
Code:
  
           Util.outputRtf   = "A basic RTF format text";
           ...
            public void Out(string str, int color)
            {
                string s;
                switch (color)
                {
                    case 1:
                        s = "\\cf2 ";
                        break;
                    case 2:
                        s = "\\cf3 ";
                        break;
                    case 3:
                        s = "\\cf4 ";
                        break;
                    case 4:
                        s = "\\cf5 ";
                        break;
                    default:
                        s = "\\cf0 ";
                        break;
                }
                s += str;
                s = s.Replace("\r\n", "\\par ");
                int ind = Util.outputRtf.IndexOf("\\cf1 #");
                Util.outputRtf = Util.outputRtf.Insert(ind, s);
                output.Rtf = Util.outputRtf;
                output.SelectionStart = output.Text.Length;
                output.ScrollToCaret();
        }
The logic is that I get a basic RTF format from a RichTextBox. I copy the ".Rtf" property value and save it to Util.outputRtf (a string inside my static Util class).
I insert a line where the ".Text" starts. The line is "\\cf1 #". Basically i use # to indicates the end of the Text abd \\cf1 makes it white, so it doesn't show.
The \\cfX changes colors from a list I have inserted in the Util.outputRtf string.

This way I can insert a string in the RichTextBox and set the color from a set of colors I have.

I could make something more complicated and general for all colors, but I don't really need it and it would be less efficient anyway.

There are some functions out there like http://www.codeproject.com/KB/edit/csexrichtextbox.aspx
C_ntua is offline   Reply With Quote
Old 12-18-2008, 03:44 PM   #9
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,279
Lol, I missed the AppendText method (slaps himself)

Edit: Thanx Magos

Last edited by C_ntua; 12-18-2008 at 04:05 PM.
C_ntua is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with C program ChrisH C Programming 38 11-13-2004 01:11 AM
how to make user text change color DarkViper Tech Board 4 12-15-2002 05:28 PM
Just one Question? Irish-Slasher C++ Programming 6 02-12-2002 10:19 AM
trying to make change from the price of the item and the amount given. please help!!! tobyman C++ Programming 2 09-04-2001 02:12 PM
is there a way to set a pixel or change font color? Flikm C++ Programming 9 09-01-2001 01:24 PM


All times are GMT -6. The time now is 07:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22