vb.net控制TextBox控制項只能輸入數值型字串

來源:互聯網
上載者:User

Module CheckTextbox<br /> '以下代碼控制TextBox控制項只能輸入數值型字串,具體內容如下:<br /> Public Sub CheckKeyPress(ByVal TargetTextBox As TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs, Optional ByVal Minus As Boolean = False, Optional ByVal DecimalCount As Integer = 0)<br /> Dim blnHandled As Boolean<br /> blnHandled = False<br /> Select Case Asc(e.KeyChar)<br /> Case Asc("-") ' 負號:只能在最前頭<br /> If Not (TargetTextBox.SelectionStart = 0 And Minus = True) Then blnHandled = True<br /> Case Asc(".") ' 小數點:小數位元大於0;在字串中沒有“.”,且加了“.”後小數位能滿足要求<br /> If DecimalCount <= 0 Then<br /> blnHandled = True<br /> Else<br /> If Not (InStr(TargetTextBox.Text, ".") = 0 And (Len(TargetTextBox.Text) - TargetTextBox.SelectionStart <= DecimalCount)) Then blnHandled = True<br /> End If<br /> Case 8 '退格鍵,<br /> Case 13 ' 斷行符號鍵<br /> SendKeys.Send("{TAB}") '轉為tab鍵<br /> Case Asc("0") To Asc("9") ' 0-9<br /> If InStr(TargetTextBox.Text, ".") > 0 Then<br /> If TargetTextBox.SelectionStart > InStr(TargetTextBox.Text, ".") - 1 Then<br /> ' 當前字元位置在小數點後,則小數點後的字元數必須小於小數位<br /> If Len(TargetTextBox.Text) - InStr(TargetTextBox.Text, ".") + 1 > DecimalCount Then blnHandled = True</p><p> End If<br /> End If<br /> Case Else<br /> blnHandled = True<br /> End Select<br /> e.Handled = blnHandled<br /> End Sub<br /> '調用如下:<br /> ' Private Sub txtJE_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtJE.KeyPress<br /> ' CheckKeyPress(sender, e, False, 0)<br /> ' End Sub<br />End Module

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.