© 2004 Microsoft Corporation. All rights reserved.

Figure 1 Modifying a Control
  Private Sub UserControl1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles MyBase.KeyPress
Dim KeyStroke As Char
Dim chrNullCharacter As Char = Chr(0)
KeyStroke = e.KeyChar
Select Case KeyStroke
Case "0" To "9", vbBack, vbCr    
'digits 0-9, backspace, return
' These keys are fine, don't do anything
Case "-"    ' minus sign
'Only one minus allowed, 
'so if there is already
'one in the textbox, throw this one away
If InStr(Me.Text, "-") <> 0 Then
KeyStroke = chrNullCharacter
End If
' if the insertion point is not sitting at zero
' (which is beginning of field), 
' throw away the minus
' sign (because it's not valid 
' except in first position)
If Me.SelectionStart <> 0 Then
KeyStroke = chrNullCharacter
End If
Case "."    ' this is a period (decimal point)
' if we already have a period, 
' throw this one away
If InStr(Me.Text, ".") <> 0 Then
KeyStroke = chrNullCharacter
End If
Case Else
' throw everything else away
KeyStroke = chrNullCharacter
End Select
If KeyStroke = chrNullCharacter Then
e.Handled = True
Else
e.Handled = False
End If
End Sub