2009年2月9日 星期一

限制 Text Box 只能輸入正數含小數 (VB 2005)

一直以為 VB 2005 的 Text Box 可以設定成只輸入數值,可是上網找了半天沒看到,
只好自己寫一段 Code 來達到目的,

在 VB2005 的 Form(frmNumericTxtBox.vb) 中加入四個 Text Box (TextBox1~4 )及一個按鈕 (Button1) 和 Lable (Label1)



在 Text Box 中只能輸入正數,其它的字元都會被過濾掉
===================================================

Public Class frmNumericTxtBox

    Private gstrTmpAs String = ""   '記錄暫存的數值

        ''將TextBox1~4 KeyDown事件都納入Handle 的範圍
    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, _
                                                                                                            TextBox2.KeyDown, _
                                                                                                            TextBox3.KeyDown, _
                                                                                                            TextBox4.KeyDown()
            ''按下任一鍵時,取出目前的內容值
            Dim myTxtBox As TextBox = Nothing
            myTxtBox = sender
            gstrTmp = myTxtBox.Text
        End Sub


        ''將TextBox1~4 TextChanged事件都納入Handle 的範圍
    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, _
                                                                                                        TextBox2.TextChanged, _
                                                                                                        TextBox3.TextChanged, _
                                                                                                        TextBox4.TextChanged()
            ''判斷是否為數字,若不是則把暫存內容值填入
            Dim myTxtBox As TextBox = Nothing
            myTxtBox = sender
            If myTxtBox.Text.Trim.Length > 0 Then
                ''限制只能輸入正數,包含小數,但不能有","
                ''因為IsNumeric 對於"," 會視為數字的一種
                ''若要有正負號則把InStr(myTxtBox.Text, "-") > 0 這個條件刪除
                If IsNumeric(myTxtBox.Text) = False Then
                            Or InStr(myTxtBox.Text, " ") > 0 _
                            Or InStr(myTxtBox.Text, ",") > 0 _
                            Or InStr(myTxtBox.Text, "-") > 0 Then
                    myTxtBox.Text = gstrTmp '若其中一個條件不合,就把暫存的值填回TextBox中
                    myTxtBox.SelectionStart = myTxtBox.Text.Length
                Else
                    gstrTmp = myTxtBox.Text.Trim
                End If
            Else
                gstrTmp = ""
            End If
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ''按下加總時
            Dim myTxtBox As TextBox = Nothing
            Dim myControl As Control = Nothing
            Dim mySum As Double = 0
            ''找出所有的Text Box 然後加總起來
            For Each myControl In Me.Controls
                If TypeOf myControl Is TextBox Then
                    myTxtBox = myControl
                    If myTxtBox.Text.Trim.Length > 0 And IsNumeric(myTxtBox.Text.Trim) = True Then
                        mySum = mySum + CType(myTxtBox.Text.Trim, Double)
                    End If
                End If
            Next
            Label1.Text = "總和: " & vbCrLf & mySum.ToString
        End Sub
End Class
=================================================

==== 2012/01/19 新增以下寫法 ====
另一個寫法是將以下的語法加入 KeyPress 的事件中
Private Sub txtBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress,TextBox2.KeyPress _
                                                                                                                    TextBox3.KeyPress,TextBox4.KeyPress
        '只允許輸入數字
        ' Chr(8) : BackSpace
        If Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> Chr(8) AndAlso e.KeyChar <> "." Then e.Handled = True
    End Sub
=================================================

2 則留言: