每次查Regular Expression查完就忘了
乾脆記下來好了....
判斷如果輸入不為數字 Return true
.NET內建的IsNumeric
會接受","當作千位分隔符號
"+"與"-"當作正負號
字串含有"."當作小數點
"e"當作科學符號
括弧、與八進制和十六進制的數值字串
所以就選用判斷式了
Private Sub txtStr1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStr1.Leave If CheckNumber(txtStr1.Text) = True Then txtStr1.Focus() End If End Sub Public Function CheckNumber(ByVal keyChar) As Boolean If System.Text.RegularExpressions.Regex.IsMatch(keyChar.ToString(), "[^0-9]") Then MessageBox.Show("請輸入數字") Return True Else Return False End If End Function
第二種方式感覺落落長指接去抓keyboard:
Private Sub txtStr1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStr1.KeyPress 'e.KeyChar == (Char)48 ~ 57 -----> 0~9 'e.KeyChar == (Char)8 -----------> Backpace 'e.KeyChar == (Char)13 -----------> Enter If e.KeyChar = Chr(48) Or _ e.KeyChar = Chr(49) Or _ e.KeyChar = Chr(50) Or _ e.KeyChar = Chr(51) Or _ e.KeyChar = Chr(52) Or _ e.KeyChar = Chr(53) Or _ e.KeyChar = Chr(54) Or _ e.KeyChar = Chr(55) Or _ e.KeyChar = Chr(56) Or _ e.KeyChar = Chr(57) Or _ e.KeyChar = Chr(13) Or _ e.KeyChar = Chr(8) Then e.Handled = False Else e.Handled = True End If End Sub
全站熱搜
留言列表