Advertisement
5_2007-2008 OLE/ COM/ DCOM/ Active-X #177811

Accept numeric values only in a textbox

It validates the entries of a textbox while it is being entered, and allows only one instance of the decimal point(period) to be entered. Please vote and leave a comment so that at least i know im doing something right. =)

AI

Tóm tắt bởi AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Mã nguồn
original-source
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
 e.Handled = NumbersOnly(e.KeyChar, TextBox1)
 End Sub
Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
 'validate the entry for a textbox limiting it to only numeric values and the decimal point
 If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point
 If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
 Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
 End If
 Return False 'for backspace
 End Function
Upload
Upload
Bình luận gốc (3)
Được khôi phục từ Wayback Machine