繁体   English   中英

如何仅在vb.net的Datagridview单元中允许一个期间

[英]How to allow one period only in Datagridview Cell in vb.net

我试图使我的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单元格只接受单个期间。

感谢您的帮助。

更好地处理您想要的事件是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),因此我提供了一个前提条件,用于检查不止一个期间的特定情况,以向您展示如何处理不同的情况(您可以可能会分析整个字符串以删除特定部分,而不是删除整个单元格)。

您只需在EditingControlShowingEventArgs事件中提供下面提到的代码,并为CONTROL_PRESS事件创建一个私有子对象,即可在DATAGRID视图中限制重复的按钮非常简单。

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准备

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM