![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 1,279
| Change RichTextBox Color 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 | |
| | #2 |
| Confused 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 | |
| | #3 |
| Registered User 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 | |
| | #4 |
| Confused 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 | |
| | #5 |
| Registered User 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 | |
| | #6 |
| Registered User 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; 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"; |
| C_ntua is offline | |
| | #7 |
| Confused 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 | |
| | #8 |
| Registered User 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();
}
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 | |
| | #9 |
| Registered User 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |