locked
How to apply multiple colors to textbox ? RRS feed

  • Question

  • How to apply multiple colors for entering text in textbox

    for example i have a string array contains (Apple,mango,banana)

    if an user enters mango as a text in textbox automatically the color of the mango text should become yellow and remaining text should not effect the yellow color.

    i tried but the color applies entire textbox.

    can anyone please help me

    Thanks,

    Balu.

    • Moved by CoolDadTx Monday, August 27, 2012 2:06 PM Winforms related (From:Visual C# General)
    Wednesday, August 22, 2012 9:43 AM

Answers

  • Hi balu,

    I would like to provide you with a simple code snippet to show how to set color in richtextbox. The text "mango" will change only when you finish the input with 'o'. You can check the text in TextChanged event.

    You can change it to meet your requirement.

            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                int selStart = richTextBox1.SelectionStart;
                int selLength = richTextBox1.SelectionLength;
                Color selColor = richTextBox1.SelectionColor;
    
                if (selStart >= 5 && richTextBox1.Text.Substring(selStart - 5, 5) == "mango")
                {
                    richTextBox1.SelectionStart = selStart - 5;
                    richTextBox1.SelectionLength = 5;
                    richTextBox1.SelectionColor = Color.DarkOrange;
                }
    
                richTextBox1.SelectionStart = selStart;
                richTextBox1.SelectionLength = selLength;
                richTextBox1.SelectionColor = selColor;
            }

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, August 30, 2012 9:59 AM

All replies

  • You have to use a Rich Text Box

    http://msdn.microsoft.com/en-us/library/3tdc88y7.aspx


    --------------------------------------------------------

    Surender Singh Bhadauria

    My Blog

     

    • Proposed as answer by Lisa Zhu Thursday, August 23, 2012 6:23 AM
    Wednesday, August 22, 2012 9:52 AM
  • you can try to use RichTextBox instead of normal one. There is a property calles selectedtext, i guess you can try to change color/font with the selectedtext and overwrite some of the events or properties.

    http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx

    regards

    joon


    • Edited by Joon84 Wednesday, August 22, 2012 9:59 AM
    • Proposed as answer by Lisa Zhu Thursday, August 23, 2012 6:23 AM
    Wednesday, August 22, 2012 9:53 AM
  • Hi Joon,

    i want to apply only to text not for selected text

    i mean when i typing text in textbox or richtextbox it automatically changes the text color

    Thanks,

    Balu.

    Monday, August 27, 2012 7:13 AM
  • Hi Joon,

    i want to apply only to text not for selected text

    i mean when i typing text in textbox or richtextbox it automatically changes the text color

    Thanks,

    Balu.

    You'll have to write code to do what you want using the RichTextBox.
    Monday, August 27, 2012 7:57 AM
  • Hi John,

    Can you please provide me an example code for that

    Monday, August 27, 2012 10:34 AM
  • hello

    Please , can any one slove this issue

    Wednesday, August 29, 2012 11:29 AM
  • Hi balu,

    I would like to provide you with a simple code snippet to show how to set color in richtextbox. The text "mango" will change only when you finish the input with 'o'. You can check the text in TextChanged event.

    You can change it to meet your requirement.

            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                int selStart = richTextBox1.SelectionStart;
                int selLength = richTextBox1.SelectionLength;
                Color selColor = richTextBox1.SelectionColor;
    
                if (selStart >= 5 && richTextBox1.Text.Substring(selStart - 5, 5) == "mango")
                {
                    richTextBox1.SelectionStart = selStart - 5;
                    richTextBox1.SelectionLength = 5;
                    richTextBox1.SelectionColor = Color.DarkOrange;
                }
    
                richTextBox1.SelectionStart = selStart;
                richTextBox1.SelectionLength = selLength;
                richTextBox1.SelectionColor = selColor;
            }

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, August 30, 2012 9:59 AM
  • Thanks Chester its working :)
    Monday, September 3, 2012 7:02 AM