modifying keypress

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
In VB6 you used to be able to modify keys as they were pressed in the
KeyPress event e.g. change it uppercase, remove certain characters etc.

How do I do this in .NET ?

Regards
Michael
 
In its simplest form:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim myChar As String
myChar = e.KeyChar
TextBox1.Text += UCase(e.KeyChar)
e.Handled = True
End Sub

You will have to enter more code to handle/not handle othe keypress events
 
Hi,
In VB6 you used to be able to modify keys as they were pressed in the
KeyPress event e.g. change it uppercase, remove certain characters etc.

How do I do this in .NET ?

Regards
Michael

Texboxes have a charactercasing property to only allow upper/lower case if
desired.
They have a keypress event, put code in there to handle the keypress, e.g.:


Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e _
As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "j" Then
'Tell the textbox the event has been handled
'and the character wont be displayed...
e.Handled = True
End If
End Sub
 
Sorry, should of been:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles

TextBox1.KeyPress
TextBox1.Text += UCase(e.KeyChar)
e.Handled = True

End Sub
 
Back
Top