Hello, I'm very new to VB, I have a small problem.

I'm simply trying to allow the user to change the ForeColor of some calculated totals that appear in Read-only text boxes using the Color Dialog box. I'm not sure what I'm doing wrong, any help would be appreciated.

Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
        'Allow user to select new color for summary totals.
        With Me.ColorDialog1
            .Color = Me.subTotalTextBox.ForeColor
            .ShowDialog()
            Me.subTotalTextBox.ForeColor = .Color
            Me.taxTextBox.ForeColor = .Color
            Me.totalTextBox.ForeColor = .Color
        End With

When I select a color the fore color remains the same. If I replace ForeColor with BackColor it works though..this seems so very simple but I just can't seem to get it to work.

Dani AI

Generated

Short answer: this is a known Windows Forms behavior — a TextBox in ReadOnly mode can fall back to system colors and your explicit ForeColor change may be ignored unless the control’s BackColor is reset. ’s observation and ’s workaround are exactly the symptom others have hit; ’s “bug” comment reflects that the TextBoxBase docs warn the ReadOnly state can override ForeColor. TextBoxBase.ForeColor (Microsoft Learn). (learn.microsoft.com)

Why this happens: TextBoxBase keeps track of whether BackColor was explicitly set (serialization/ShouldSerialize logic). Once BackColor is set, the control no longer switches to the system ReadOnly color automatically — so simply setting ForeColor later may not take effect. You can restore the default behavior by resetting the BackColor (either ResetBackColor() or BackColor = Color.Empty) so the ReadOnly/color logic runs again. See the discussion and examples showing the Reset/Color.Empty trick. (stackoverflow.com)

Practical options:

  • Use a Label for display-only totals (simplest if selection/copy isn’t required).
  • If you need selectable text, keep the TextBox ReadOnly and reset its BackColor before applying ForeColor. Example VB pattern:
Dim cd As New ColorDialog()
If cd.ShowDialog() = DialogResult.OK Then
    For Each tb As TextBox In {subTotalTextBox, taxTextBox, totalTextBox}
        tb.ResetBackColor()           'clear any explicit BackColor so ReadOnly rules apply
        tb.ForeColor = cd.Color
    Next
End If

Also consider deriving a custom TextBox and handling painting (or overriding BackColor serialization) if you must guarantee exact colors in every state — the OnPaint/UserPaint route is a common advanced fix. (syncfusion.com)

Notes: check the ColorDialog result before applying colors (shown above). ResetBackColor() may not show in IntelliSense but is a supported way to return the property to its default (or use BackColor = Color.Empty). Choose Label vs ReadOnly TextBox based on whether users must copy/select the totals.

Recommended Answers

All 3 Replies

Hello, I'm very new to VB, I have a small problem.

I'm simply trying to allow the user to change the ForeColor of some calculated totals that appear in Read-only text boxes using the Color Dialog box. I'm not sure what I'm doing wrong, any help would be appreciated.

Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
        'Allow user to select new color for summary totals.
        With Me.ColorDialog1
            .Color = Me.subTotalTextBox.ForeColor
            .ShowDialog()
            Me.subTotalTextBox.ForeColor = .Color
            Me.taxTextBox.ForeColor = .Color
            Me.totalTextBox.ForeColor = .Color
        End With

When I select a color the fore color remains the same. If I replace ForeColor with BackColor it works though..this seems so very simple but I just can't seem to get it to work.

I got it working but I'd still appreciate if someone can chime in. After putting Google to use I've found that the Fore Color of Read Only text boxes are ignored unless the Back Color is also set. So I changed my code to:

Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal(e As System.EventArgs) Handles ColorToolStripMenuItem.Click
        'Allow user to select new color for summary totals.
        With Me.ColorDialog1
            .Color = Me.subTotalTextBox.ForeColor
            .ShowDialog()
            Me.subTotalTextBox.BackColor = Me.subTotalTextBox.BackColor
            Me.taxTextBox.BackColor = Me.taxTextBox.BackColor
            Me.totalTextBox.BackColor = Me.totalTextBox.BackColor
            Me.subTotalTextBox.ForeColor = .Color
            Me.taxTextBox.ForeColor = .Color
            Me.totalTextBox.ForeColor = .Color
        End With
    End Sub

and now I'm getting the results I expected.

Is this the only work around for this? It is working now but if I'm taking extra steps to accomplish something that can be done easier, I'd like to know. Thanks. :)

>I got it working but I'd still appreciate if someone can chime in.

It is a BUG.

' Working with and around the bug
' Remove some code redundancy with a function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cd As New ColorDialog
        If cd.ShowDialog = Windows.Forms.DialogResult.OK Then
            ChangeTextboxForeColor(txt1, cd.Color)
            ChangeTextboxForeColor(txt2, cd.Color)
            ChangeTextboxForeColor(txt3, cd.Color)
        End If
    End Sub
    Private Sub ChangeTextboxForeColor(ByVal txtName As TextBox, ByVal SelectedColor As System.Drawing.Color)
        With txtName
            .BackColor = .BackColor
            .ForeColor = SelectedColor
        End With
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.