Restore default color to a control?

ccbryan

Active member
Joined
Oct 14, 2008
Messages
30
Programming Experience
5-10
Hello. I am building a data entry form. I have code that runs when the 'submit' button is clicked to check that all required fields are populated; if not, the missing fields are hilighted in red:

VB.NET:
Sub validatefields()
    saveOK = True
    If Trim(Me.cmbBillCat.Text) = "" Then
        saveOK = False
        Me.cmbBillCat.Appearance.BackColor = Color.Tomato
    End If
    etc....

My problem arises when I want to reset the color back to normal. In my form when you click Cancel or move to a new record, all fields are disabled (set to read only) until the 'Edit' button is clicked. Visually this is represented by the field backgrounds being gray. VB.Net automatically changes the text box/combo boxes backcolor to gray when I disable them. However, once I have explicitly changed a control's backcolor, thereafter disabling it does not change its color to gray.

VB.NET:
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As     
    System.EventArgs) Handles btnCancel.Click
        Me.cmbBillCat.Appearance.BackColor = Color.White       
        DisableFields()   
        Me.Refresh() 
End Sub

Sub DisableFields()
        Me.cmbBillCat.ReadOnly = True
End Sub


I have tried changing the backcolor to System.Drawing.SystemColors.Window, but the problem is the same. The control whose color has been changed is disabled (won't accept entry) but the backcolor remains white, no matter how many times DisableFields() is issued. So I end up with all the fields on the form being gray but one.

Any ideas? Thanks in advance...
 
Last edited:
Well the easiest & most flexible way I can think of would be to store the original color in a variable before you change it to something else, then when you need to change it back, just set it back to that variable.
 
I have tried changing the backcolor to System.Drawing.SystemColors.Window, but the problem is the same. The control whose color has been changed is disabled (won't accept entry) but the backcolor remains white
This is probably due to the ambience of that property, default values are only provided when the value is not explicitly set. To reset it you can set it to Color.Empty or TextBox.DefaultBackColor.
 
X-lent!

This is probably due to the ambience of that property, default values are only provided when the value is not explicitly set. To reset it you can set it to Color.Empty or TextBox.DefaultBackColor.

Thanks JohnH, Color.Empty was exactly what I was looking for...
 
Back
Top