My problem with NumericUpDown control is when the user selects a value from NumericUpDown And unchecks the checkBox1 and clicks the Save button, the value of NumericUpDown not c
I was using the NumericUpDown control without the spinners because I wanted only numbers. The control was in a child dialog that was invoked from a listview selection. When the value to be loaded into the NumericUpDown control was zero in the listwiew, a zero was in the control. That zero always had to be cleared out. Here's how I made it "appear" blank. If it was zero, I change the ForeColor of the control to white. I added an Enter event that changed the ForeColor to black and deleted the zero.
This hid the NumericUpDown control spinners.
AccessCode.Controls[0].Visible = false;
These statements were in the form initialization. (var code was to be loaded into AccessCode)
if (code == 0)
{
AccessCode.ForeColor = Color.White;
}
The Enter event code looked like this.
private void AccessCode_Enter(object sender, EventArgs e)
{
if (AccessCode.ForeColor == Color.White)
{
AccessCode.ForeColor = Color.Black;
AccessCode.Focus();
SendKeys.SendWait("{Delete}");
}
}
The AccessCode control also had a Leave event that checked for valid data.
NumericUpDown does not contain string. It has value as numbers. So it should be:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = False Then
AgeNumericUpDown.Value = 0
End If
End Sub
EDIT:
Check this out then if you don't want the value to be zero, but empty!
Private Sub BttnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttnSave.Click
If AgeNumericUpDown.Value <> 0 Then
AgeNumericUpDown.Controls(1).Text = ""
End If
End Sub
Access the text control of the NumericUpDown
and then set text to be blank
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = False Then
AgeNumericUpDown.Controls.Item(1).Text = ""
End If
End Sub