Number-Only Text Box
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
Me.TextBox1.Location = New System.Drawing.Point(64, 64)
Me.TextBox1.Size = New System.Drawing.Size(208, 20)
Me.TextBox1.Text = ""
'
Me.Label1.Location = New System.Drawing.Point(8, 16)
Me.Label1.Size = New System.Drawing.Size(328, 23)
Me.Label1.Text = "Numbers, a colon, dash and space:"
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(344, 149)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.TextBox1})
Me.Text = "Creating your Own Number-Only Text Box"
Me.ResumeLayout(False)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim strAllowableChars As String
strAllowableChars = "0123456789-: "
If InStr(strAllowableChars, e.KeyChar.ToString) = 0 Then
e.Handled = True
End If
End Sub
End Class
Related examples in the same category