输入字母而不是数字时出现错误消息

时间:2014-11-21 13:19:43

标签: vb.net

我是vb.net&的新手。我有一个问题,即每当用户输入字母表时,他/她将收到仅允许数字的消息。对于此代码....请帮助我。我将非常感谢你。

Public Class Form1
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    If DataGridView1.CurrentCell.ColumnIndex = 0 Then
        Dim combo As ComboBox = CType(e.Control, ComboBox)
        If (combo IsNot Nothing) Then

            RemoveHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)

            AddHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
        End If
    End If
End Sub

Private Sub ComboBox_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim combo As ComboBox = CType(sender, ComboBox)
    If combo.SelectedItem = "Item1" Then
        DataGridView1.CurrentRow.Cells(1).Value = "KG"
        DataGridView1.CurrentRow.Cells(3).Value = "100"
        DataGridView1.CurrentRow.Cells(2).Value = "Raw Material"
    ElseIf combo.SelectedItem = "Item2" Then
        DataGridView1.CurrentRow.Cells(1).Value = "Liter"
        DataGridView1.CurrentRow.Cells(3).Value = "47"
        DataGridView1.CurrentRow.Cells(2).Value = "Raw Material"
    ElseIf combo.SelectedItem = "Item3" Then
        DataGridView1.CurrentRow.Cells(1).Value = "Pound"
        DataGridView1.CurrentRow.Cells(3).Value = "54"
        DataGridView1.CurrentRow.Cells(2).Value = "Raw Material"
    End If
End Sub
Private Sub Mul_Button_Click(sender As Object, e As EventArgs) Handles Mul_Button.Click
    Dim s As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(3).Value)
    Dim s1 As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(4).Value)
    DataGridView1.CurrentRow.Cells(5).Value = s * s1
End Sub

Private Sub DataGridView1_CellValidated(sender As Object, e As EventArgs) Handles DataGridView1.CellValidated
    Dim s As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(3).Value)
    Dim s1 As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(4).Value)
    DataGridView1.CurrentRow.Cells(5).Value = s * s1
    If DataGridView1.RowCount > 0 Then
        Dim sum As Integer
        For index As Integer = 0 To DataGridView1.RowCount - 1
            sum += Convert.ToInt32(DataGridView1.Rows(index).Cells(5).Value)
        Next
        TextBox1.Text = sum
    End If
End Sub

Private Sub Add_Button_Click(sender As Object, e As EventArgs) Handles Add_Button.Click
    If DataGridView1.RowCount > 0 Then
        Dim sum As Integer
        For index As Integer = 0 To DataGridView1.RowCount - 1
            sum += Convert.ToInt32(DataGridView1.Rows(index).Cells(5).Value)
        Next
        TextBox1.Text = sum
    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

使用Try / Catch块包装具有Convert.ToInt16()调用的代码,或者将它们转换为Int16.TryParse()方法。

例如,这一行:

Dim s As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(3).Value)

可能成为:

Dim s As Int16
Dim strValue As String = DataGridView1.CurrentRow.Cells(3).Value
If Int16.TryParse(strValue, s) Then

    ' ... do something with "s" in here ...
    ' ... continue with code...

Else
    MessageBox.Show(strValue, "Invalid Value")
End If