簡體   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