How to change Font colour of a few Strings only?

VKSB 236 Reputation points
2024-06-08T13:42:33.5833333+00:00

Hi all,

I am working on a program that gives "Planetary positions" for a period of time ( eg: few weeks, few months). As all the planets except Sun & Moon have "Retrograde motion" (Apparent backward movement), I want to show the planet's positions in a different colour font when it is in "Retrograde Motion".

I am using "TextBox" / "RichTextBox" to show the "Planets' Positions".

I tried the following code to show the "Retro" Positions in "Dark Green" font & while the rest of the Position in "Navy Blue" font. This shows the "Retro" Positions in Dark Green, but when the "Retro" positions changes to normal one, the entire display changes back to the "Navy Blue", the "Dark Green" Font changes back to "Navy Blue".

Could you please help me on this?

I want the Planet's Positions in "Normal Motion" are displayed in "Navy" colour font & the Planet's Positions in the "Retrograde Motion" are displayed in Dark Green Font.

Thanks

VKSB

Dim PlanetPositions_Arrays(99) As Double
Dim PlanetSpeed_Arrays(99) As Double
Dim SelectedPlanet As String = ""

        For i = 0 To 99
            PlanetPositions_Arrays(i) = Planet(0) 'Planet Positions
            PlanetSpeed_Arrays(i) = Planet(3)  ' Planet Speed

            If PlanetSpeed_Arrays(i) < 0 Then
                TxtBox1.ForeColor = Color.DarkGreen
                TxtBox1.AppendText(SelectedPlanet & " Position:" & " = " & (PlanetPositions_Arrays(i)) & " = " & " Planet Speed : " & PlanetSpeed_Arrays(i) & vbCrLf)

            ElseIf PlanetSpeed_Arrays(i) > 0 Then
                TxtBox1.ForeColor = Color.Navy
                TxtBox1.AppendText(SelectedPlanet & " Position:" & " = " & (PlanetPositions_Arrays(i)) & " = " & " Planet Speed : " & PlanetSpeed_Arrays(i) & vbCrLf)

            End If

        Next i

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 50,506 Reputation points
    2024-06-08T14:53:04.7566667+00:00

    Unfortunately your code won't work. Setting the base control's ForeColor, BackColor will reset the coloring for everything. In order to change the color of just a subset of the text you have to use the SelectionColor property. To get this to work you'd need to set the selection color and then select the text. Perhaps something like this useful extension method.

    Module RichTextBoxExtensions
        <Extension>
        Public Sub AppendTextWithColor(control As RichTextBox, text As String, color As Color)
            control.SelectionColor = color
    
            ' Ensure nothing selected
            control.SelectionLength = 0
            control.AppendText(text)
    
            ' Reset
            control.SelectionColor = control.ForeColor
        End Sub
    End Module
    

    Note that there are other Selection... properties to change other aspects.

    To use this method just replace the calls to AppendText.

    TextBox1.AppendTextWithColor("Navy", Color.Navy)
    TextBox1.AppendText(" ")
    TextBox1.AppendTextWithColor("DarkGreen", Color.DarkGreen)
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Swarit patel(Microsoft) 0 Reputation points
    2024-06-08T14:13:33.0066667+00:00

    (<p style="font-size:14px; color:#538b01; font-weight:bold; font-style:italic;"> ) Paste this code you problem is solved