Clearing NumericUpDown

大憨熊 提交于 2019-12-02 23:29:31

问题


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 cleared:

 Public Class FormAdd


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.StudenttableBindingSource.AddNew()
End Sub

Private Sub BttnSave_Click(sender As Object, e As EventArgs) Handles BttnSave.Click

    Me.StudenttableBindingSource.EndEdit()
    Me.StudenttableTableAdapter.Update(Me.StudentDataSet.studenttable)
    Me.StudenttableTableAdapter.Fill(Me.StudentDataSet.studenttable)
    MsgBox(" Student Saveed", MsgBoxStyle.Information)
    Me.StudenttableTableAdapter.Fill(Main.StudentDataSet.studenttable)
    Me.Close()
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked = False Then
        AgeNumericUpDown.Value = AgeNumericUpDown.Minimum
        AgeNumericUpDown.Text = ""
    End If
End Sub

I need if the user selects a value and unchecks the CheckBox1 value of AgeNumericUpDown to reset to an empty string.

Illustrated:


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/21831150/clearing-numericupdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!