I know this should be easy, but I am stumped at this point. I have a custom TextBox that has a property ControlState. ControlState points to an Enum with values like Normal, Warning, Error, etc. When ControlState gets changed, it paints the BackColor accordingly. That works great. Where my problem comes in is when the user has changed the BackColor at design time to say Green. When a Warning state occurs and the BackColor is set to say Yellow and then the Warning state is fixed, I want to paint it back to Green. However, I cannot figure out where to pickup the color that was selected at design time (or the default color if nothing was set at design time).
Here are some things I have tried:
Code that applies to all of the scenarios:
Code:<System.ComponentModel.Description("Sets the control state to Warning or Error. Changes the border color based upon the selection."), System.ComponentModel.Category("Validation")> _ Property ControlState() As ControlStates Get Return _ControlState End Get Set(ByVal value As ControlStates) _ControlState = value Me.BackColor = GetBackgroundColor() End Set End Property Private Function GetBackgroundColor() As Color Dim RetVal As Color If Not Me.Enabled Then RetVal = System.Drawing.SystemColors.Control Else Select Case True Case Me.ControlState = ControlStates.Warning RetVal = Color.PaleGoldenrod Case Me.ControlState = ControlStates.Error RetVal = Color.LightCoral Case Me.IsRequired RetVal = Color.OldLace Case Me.ReadOnly RetVal = Color.FromArgb(245, 245, 245) Case Else RetVal = _BackColor End Select End If Return RetVal End Function
In this scenario, I was trying to catch the BackColor the first time it was changed (Static) and then set MyBase.BackColor. This would never change the BackColor of the control. I am guessing that since I am changing the color of MyBase and not Me, that is creating my situation.
Code:Private _BackColor As Color Overrides Property BackColor As Color Get Return _BackColor End Get Set(value As Color) Static bDone As Boolean If Not bDone Then _BackColor = value bDone = True End If MyBase.BackColor = value End Set End Property
I tried to override the OnBackColorChanged event to catch it first being set. This worked if you set it to something other than default at design time. If you left it at default, the static variable didn't get set until the first time the BackColor was changed in the app. That would mean that it has the wrong color to set it back to when set back to Normal.
Thanks in advance!!Code:Protected Overrides Sub OnBackColorChanged(e As EventArgs) MyBase.OnBackColorChanged(e) Static bDone As Boolean If Not bDone Then _BackColor = Me.BackColor bDone = True End If End Sub




Reply With Quote