none
In Native UWP, How to apply color for a particular character in a string ? RRS feed

  • Question

  • My requirement is I like to display some string in the textbox control and I want to apply color for a particular character in a string. Please give me a possible suggestion to achieve my requirement.
    Thursday, July 11, 2019 7:14 AM

All replies

  • Hi,

    You need to use RichEditBox to change the color of a particular character. Like this:

       private void Button_Click(object sender, RoutedEventArgs e)
            {
                //get string length
                string textStr;
                Mytext.Document.GetText(TextGetOptions.None, out textStr);
                var strLenght = textStr.Length;
                //search for specific character
                string charText;
                for (int i = 1; i < strLenght; i++)
                {
                    ITextRange textRange = Mytext.Document.GetRange(i - 1, i);
                    textRange.GetText(TextGetOptions.None, out charText);
                    if (charText.Equals("a"))
                    {
                        //change color
                        textRange.CharacterFormat.ForegroundColor = Colors.Red;
                    }
                }
            }

    For more information, please refer the document:RichEditBox Class

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 11, 2019 9:55 AM
    Moderator
  • Hi Roy,

    Thanks for your valuable comments. But in my case I am preparing a custom control by inheriting the TextBox control in which I need to apply color for a particular character in a string. Since I am not using RichTextBox control I can't apply the above solution So  I need any other possible solution to achieve my requirement using TextBox control.

     

    Thursday, July 11, 2019 1:52 PM
  • Hi,

    TextBox could not change the color of particular characters in code. That's why I'd suggest you to use the RichEditBox.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 12, 2019 1:49 AM
    Moderator