none
Why this output is missing a character and the color didn't change? RRS feed

  • Question

  • Dim index1 As String = "11"

    RichTextBoxCalender.Text = index1(ForeColor = Color.Red)

    Just for test, I ran this code then the richtextbox just displayed an one-place "1" with system default font.

    I wonder what's wrong and how to set a particular color of part of a string that differs to the rest of the string in a richtextbox.

    Thank you.

    Sunday, June 17, 2018 3:17 AM

Answers

  • Dim index1 As String = "11"

    RichTextBoxCalender.Text = index1(ForeColor = Color.Red)

    Just for test, I ran this code then the richtextbox just displayed an one-place "1" with system default font.

    I wonder what's wrong and how to set a particular color of part of a string that differs to the rest of the string in a richtextbox.

    Thank you.

    If we break this down, your code does the following:

    1. Set variable "index1" to the string "11"
    2. Make a Boolean comparison of (ForeColor = Color.Red)
      Since you just used ForeColor without specifying a control, it has defaulted to the Form.ForeColor as that is the only ForeColor property in scope.  This value (unless you changed it) is equal to SystemColors.WindowText which is most likely black.  Therefore, (ForeColor = Color.Red) would resolve to the Boolean value False.
    3. Access a character from the string "index1".
      When you write "index1()", you are accessing the character array that makes up the string.  The value in the parentheses is the index within the character array you want to access.  In this case you passed False as the index.  But since an Index is an integer value, an implicit conversion occurs to convert the Boolean value into an integer.  The integer value of False is 0.
    4. Set the RichTextBoxCalender.Text to the first character of the index1 string.
      The first character of the string is "1" so that is what appears in your textbox.

    To accomplish what you expected, you will need to set the text, select desired character range and then set the selection color.

    So your code should be something like:

    Dim index1 As String = "11"
    RichTextBoxCalender.Text = index1
    RichTextBoxCalender.SelectionStart = 1
    RichTextBoxCalender.SelectionLength = 1
    RichTextBoxCalender.SelectionColor = Color.Red
    This will set the text to "11", select the second character, and change its color to Red.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Felix_Huang Monday, July 16, 2018 3:48 AM
    Tuesday, June 26, 2018 11:42 AM
    Moderator

All replies

  • Hi

    You can try the following method:

     Dim index1 As String = "11"
      RichTextBoxCalender.SelectionColor = Color.Red
      RichTextBoxCalender.AppendText(index1)

    Best Regards

    Alex


    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.

    Tuesday, June 26, 2018 5:31 AM
  • Dim index1 As String = "11"

    RichTextBoxCalender.Text = index1(ForeColor = Color.Red)

    Just for test, I ran this code then the richtextbox just displayed an one-place "1" with system default font.

    I wonder what's wrong and how to set a particular color of part of a string that differs to the rest of the string in a richtextbox.

    Thank you.

    If we break this down, your code does the following:

    1. Set variable "index1" to the string "11"
    2. Make a Boolean comparison of (ForeColor = Color.Red)
      Since you just used ForeColor without specifying a control, it has defaulted to the Form.ForeColor as that is the only ForeColor property in scope.  This value (unless you changed it) is equal to SystemColors.WindowText which is most likely black.  Therefore, (ForeColor = Color.Red) would resolve to the Boolean value False.
    3. Access a character from the string "index1".
      When you write "index1()", you are accessing the character array that makes up the string.  The value in the parentheses is the index within the character array you want to access.  In this case you passed False as the index.  But since an Index is an integer value, an implicit conversion occurs to convert the Boolean value into an integer.  The integer value of False is 0.
    4. Set the RichTextBoxCalender.Text to the first character of the index1 string.
      The first character of the string is "1" so that is what appears in your textbox.

    To accomplish what you expected, you will need to set the text, select desired character range and then set the selection color.

    So your code should be something like:

    Dim index1 As String = "11"
    RichTextBoxCalender.Text = index1
    RichTextBoxCalender.SelectionStart = 1
    RichTextBoxCalender.SelectionLength = 1
    RichTextBoxCalender.SelectionColor = Color.Red
    This will set the text to "11", select the second character, and change its color to Red.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Felix_Huang Monday, July 16, 2018 3:48 AM
    Tuesday, June 26, 2018 11:42 AM
    Moderator
  • Thank you very much!!!!

    It's really useful. No wonder I see many codes about Length that confusing me when I search on Internet.

    Monday, July 16, 2018 3:50 AM