I'm building a form that captures number of hours worked. This is a rewrite of a legacy application that didn't use proper controls to capture input, so one of the things that I'm doing is replacing TextBox controls used for capturing the hours with NumericUpDown controls instead.
I need some custom logic for these controls that deviate slightly from the default UI. For example, I can accept up to 2 decimal places, but it should show the least number of decimals possible. The other thing is that if the value is zero, then don't show any value.
In the designer, I have each NumericUpDown's BackgroundColor and ForeColor set to Window since their value starts at zero and then there's logic in the Enter/Leave that changes the ForeColor:
Code:
Private Sub NumericUpDowns_Enter(sender As Object, e As EventArgs) Handles NumericUpDownHours.Enter, NumericUpDownOvertime.Enter, NumericUpDownPremiumPay.Enter
Dim nud As NumericUpDown = TryCast(sender, NumericUpDown)
If (nud Is Nothing) Then
Return
End If
nud.DecimalPlaces = 2 ' allow for 2 decimals
nud.ForeColor = ForeColor ' show the value to the user
Dim displayedValue As String = nud.Value.ToString("F2")
nud.Select(0, displayedValue.Length) ' select the control
End Sub
Private Sub NumericUpDowns_Leave(sender As Object, e As EventArgs) Handles NumericUpDownHours.Leave, NumericUpDownOvertime.Leave, NumericUpDownPremiumPay.Leave
Dim nud As NumericUpDown = TryCast(sender, NumericUpDown)
If (nud Is Nothing) Then
Return
End If
If (nud.Value = 0) Then
' hide the value from the user
nud.ForeColor = nud.BackColor
End If
Dim decimalPlaces As Integer = 0 ' show 0 decimal places
Dim value As String = nud.Value.ToString("F2")
If (value(value.Length - 1) <> "0"c) Then
decimalPlaces = 2 ' show 2 decimal places
ElseIf (value(value.Length - 2) <> "0"c) Then
decimalPlaces = 1 ' show 1 decimal places
End If
nud.DecimalPlaces = decimalPlaces
End Sub
The problem I'm running into is that NumericUpDownPremiumPay's Enabled property is driven by the state of a checkbox and when the NumericUpDown is disabled then the ForeColor is the disabled color instead of the BackColor.
If you look at this picture, now how Hours and Overtime look empty, but Premium Pay does not.
Edit - Eff it, I'm tired of trying to get the image to render in thread. Just open the link and get redirected I guess.