Private Sub txtRatio_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRatio.KeyPress '輸入為數字,小數點或backspace If Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(8) Then 'Backspace: '輸入的小數點為唯一 If e.KeyChar = "." And InStr(CType(sender, System.Windows.Forms.TextBox).Text, ".") > 0 Then e.Handled = True Else '小數最多2位 If e.KeyChar <> Chr(8) And InStr(CType(sender, System.Windows.Forms.TextBox).Text, ".") > 0 Then Dim sAry() As String = CType(sender, System.Windows.Forms.TextBox).Text.Split(".") If sAry(1).Length >= 2 Then e.Handled = True Else e.Handled = False End If Else e.Handled = False End If End If '輸入的負號是否在第一位 ElseIf e.KeyChar = "-" And CType(sender, System.Windows.Forms.TextBox).Text = "" Then e.Handled = False Else e.Handled = True End If End Sub
10/19/2010
vb.net限制輸入數字並只到小數點後2位
Labels:
.net,
MS Visual Studio 2005,
vb
訂閱:
張貼留言 (Atom)
太棒了, 謝謝分享
回覆刪除