0

我试图让我的 datagridview 单元格只接受数字和一个句点。

到目前为止,我已经成功使它只接受数字,这里是代码:

    Select Case e.KeyChar
        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", vbBack
            e.Handled = False
        Case Else
            e.Handled = True
    End Select

在我的文本框中,我也将只接受数字和单个句点,这是代码:

    Select Case e.KeyChar
        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", vbBack
            e.Handled = False
        Case Else
            e.Handled = True
    End Select


    If (txt1.Text.IndexOf(".") >= 0 And e.KeyChar = ".") Then e.Handled = True

所有代码都在 KeyPress 事件中。我不知道如何让我的 datagridview 单元格只接受单个句点。

感谢您的帮助。

4

2 回答 2

1

更好地处理您想要的事件是CellValueChanged:它只会检查最终值并最终更正它。您也可以依靠IsNumeric快速找到有效号码。示例代码DataGridView1

Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

    If (e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0) Then

        Dim curVal As String = DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString()
        If (curVal.Trim().Length > 0) Then

            If (curVal.Contains(".")) Then
                'Checking whether the given entry has more tha one period

                Dim temp() As String = curVal.Split("."c)
                If (temp.Length > 2) Then
                  'More than one period
                   DataGridView1(e.ColumnIndex, e.RowIndex).Value = temp(0) & "." & temp(1)
                ElseIf (Not IsNumeric(curVal)) Then
                   'Is not numeric
                   DataGridView1(e.ColumnIndex, e.RowIndex).Value = ""
                End If

            ElseIf (Not IsNumeric(curVal)) Then
                'Any other non-numeric entry
                DataGridView1(e.ColumnIndex, e.RowIndex).Value = ""
            End If

       End If

    End If

End Sub

请记住,这IsNumeric将捕获任何非数字情况(例如:1.32.52),因此我包含了一个先决条件来检查具有多个时期的特定情况,以向您展示如何处理不同的情况(您可能分析整个字符串以删除特定部分,而不是只删除整个单元格)。

于 2013-10-11T08:23:23.023 回答
0

在 DATAGRID 视图中限制重复期间按钮非常简单,您只需在 EditingControlShowingEventArgs 事件中提供以下提到的代码并为 CONTROL_PRESS 事件创建一个私人子。

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    If TypeOf e.Control Is TextBox Then
        Dim tb As TextBox = TryCast(e.Control, TextBox)

        RemoveHandler tb.KeyPress, AddressOf CONTROL_KEYPRESS
        If Me.DataGridView1.CurrentCell.ColumnIndex = 3 Or Me.DataGridView1.CurrentCell.ColumnIndex = 4 Then
            AddHandler tb.KeyPress, AddressOf CONTROL_KEYPRESS
        End If

    End If

End Sub

Private Sub CONTROL_KEYPRESS(ByVal SENDER As TextBox, ByVal E As KeyPressEventArgs)

    If (Not Char.IsControl(E.KeyChar) And Not Char.IsDigit(E.KeyChar) And E.KeyChar <> "."c) Then
        E.Handled = True
    End If
    If (E.KeyChar = "."c And SENDER.Text.IndexOf("."c) > -1) Then
        E.Handled = True

    End If

End Sub

由 FURQAN HALEEM 准备

于 2015-01-09T05:06:50.660 回答