2013-10-11 55 views
0

我想讓我的datagridview單元格只接受數字和單個句點。如何只允許一個時期在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單元格只接受單個句點。

謝謝你的幫助。

+0

,在你的第二個代碼塊的最後一行應該這樣做。在此之後你有其他的代碼可能將處理設置回false嗎? – Steve

回答

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),因此我已經包括一個先決條件檢查的具體情況與超過一個週期向您展示如何處理不同的情況(您可能會分析整個字符串以刪除特定部分,而不是刪除整個單元格)。

+0

@varcobas:嗨,先生,謝謝你的回答,從這段代碼我現在已經創建了一個工作代碼。對我的幫助很大,再次感謝你:) – Matthew

+0

@Matthew(我的暱稱是varocarbas)不客氣。 – varocarbas

+0

抱歉,關於 – Matthew

0

它很容易限制DAGAGRID中的重複期間按鈕查看您只需提供在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 

PREPED BY FURQAN HALEEM

相關問題