2013-10-11 44 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

相关问题