Jump to content
  • Advertisement
Sign in to follow this  
EvilNando

[.net] Textbox.Text multicolored , is it possible?

This topic is 3721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to display a string over a textbox but would like to color specific words of the text with diferent colors. Is this possible? I only know of the BackColor property which applies to the entire control's Text If there is a simple solution to this I would love to know, but if its too much work then black text and Ill leave it at that thank you guys!

Share this post


Link to post
Share on other sites
Advertisement
Just found out

if you are interested in this subject the solution is here
http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/651faf9b-ae32-4c99-b619-d3afd89477e1

Share this post


Link to post
Share on other sites
Yes you can do it but you have to override the onPaint and do an owner draw. If you really want to see an example, let me know I will throw something together.

theTroll

Share this post


Link to post
Share on other sites
yep I ended using richtextbox
at the end I ended solving it myself, heres the code

just in case anyone cares to read about it


private void FormatXmlText(string[] XmlText)
{
int line_count = 0;

// dynamic line numbering
foreach (String line in XmlText)
{
String numbering = line_count.ToString();

for (int x = 0; x < XmlText.GetLength(0).ToString().Length; x++)
{
numbering += " ";
}

parent_form.rch_txtbox.Text += numbering + " " + line;

line_count++;
}

String[] lines = parent_form.rch_txtbox.Text.Split(Convert.ToChar("\n"));

int processed_chars = 0;


// syntax colorization
foreach (String line in lines)
{
bool delimiter_found = false;
parent_form.rch_txtbox.SelectionLength = 0;

char[] char_array = line.ToCharArray();

for (int char_pos = 0; char_pos < char_array.GetLength(0); char_pos++)
{
if (char_pos == 0)
{
// line numbering
parent_form.rch_txtbox.SelectionStart = processed_chars;
parent_form.rch_txtbox.SelectionLength = char_pos.ToString().Length;
parent_form.rch_txtbox.SelectionColor = System.Drawing.Color.RoyalBlue;
}

if (char_array[char_pos] == '>' )
{
parent_form.rch_txtbox.SelectionStart = char_pos + processed_chars + 1;
delimiter_found = true;
}

if (delimiter_found)
{
if (char_array[char_pos] == '<' && char_array[char_pos + 1] == '/' && char_array[char_pos - 1] != ' ')
{
parent_form.rch_txtbox.SelectionLength -= 2;
parent_form.rch_txtbox.SelectionColor = System.Drawing.Color.Black;
delimiter_found = false;
}
else
{
parent_form.rch_txtbox.SelectionLength++;
}

}
}

processed_chars += char_array.GetLength(0) + 1;

}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!