TextBox_KeyPress event

G

Guest

Sometimes it is useful to intercept keyboard entry and take some action on
the actual entry to make it more useful, etc. How do I duplicate the
functionality of the following VB6 code to VB.Net 2005 code? (if you enter u
or U in txtAccountNumber, rather than a "u" or "U", a "1" appears.)

Private Sub txtAccountNumber_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 85 'U, change to 1
KeyAscii = 49
Case 117 'u, change to 1
KeyAscii = 49
Case Else
' do nothing, all entry except U or u is OK
End Select
End Sub

Thanks, John Brown
 
A

Armin Zingler

John Brown said:
Sometimes it is useful to intercept keyboard entry and take some
action on the actual entry to make it more useful, etc. How do I
duplicate the functionality of the following VB6 code to VB.Net 2005
code? (if you enter u or U in txtAccountNumber, rather than a "u" or
"U", a "1" appears.)

Private Sub txtAccountNumber_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 85 'U, change to 1
KeyAscii = 49
Case 117 'u, change to 1
KeyAscii = 49
Case Else
' do nothing, all entry except U or u is OK
End Select
End Sub


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

Select Case e.KeyChar
Case "U"c, "u"c
e.KeyChar = "1"c
End Select

End Sub


Armin
 
M

Michel Vanderbeke

Hello,

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

Select Case e.KeyChar
Case "U"c, "u"c
e.KeyChar = "1"c
End Select

End Sub

I wondered, what is the meaning of the 'c' after "U", "u" and "1"?

Thank you,

Michel
 
A

Armin Zingler

Michel Vanderbeke said:
Hello,



I wondered, what is the meaning of the 'c' after "U", "u" and "1"?


It's a suffix determining a character (Char) literal.

http://msdn2.microsoft.com/en-us/library/s9cz43ek(VS.71).aspx

I've looked for the link in the language specification but in the MSDN lib
TOC it's only still there vor VS 2003:
http://msdn2.microsoft.com/en-us/library/s9cz43ek(VS.71).aspx

For VB 2005, it can be downloaded here:
http://msdn2.microsoft.com/en-us/library/ms234437(VS.80).aspx


Armin
 
G

Guest

Thanks for your suggestion Armin.

However, I get the error "Property 'KeyChar' is 'ReadOnly' when I use your
code example.
 
A

Armin Zingler

John Brown said:
Thanks for your suggestion Armin.

However, I get the error "Property 'KeyChar' is 'ReadOnly' when I
use your code example.


Strange, I don't know why. It's not declared Readonly anywhere.

Right-click on 'keychar' and select "go to definition". After the object
browser opened, press Ctrl+C to copy the full name, then paste it here in
your next post.

I get "System.Windows.Forms.KeyPressEventArgs.KeyChar".



Armin
 
G

Guest

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

Doesn't "ByVal" indicate that "e" is read only?
(If I change it to "ByRef" I get more errors.)
 
J

Jack Jackson

No. ByVal means that if you change e to point to something else:

e = New System.Windows.Forms.KeyPressEventArgs

then the caller will not see that new instance of e. But it does not
mean that you can't change the properties of e.
 
G

Guest

Jack,

That is what I thought originally but I was trying to figure out why I was
getting the error "Property 'KeyChar' is 'ReadOnly' when I say:

e.KeyChar = "1"c

Do you have any ideas about a solution to the original question for this
post Jack?

Thanks,
 
G

Guest

Armin,

I think I see something after following your instructions:
For:
KeyPressEventArgs[Compact Framework]

System.Windows.Forms.KeyPressEventArgs.KeyChar

Public ReadOnly Property KeyChar() As Char
Member of: System.Windows.Forms.KeyPressEventArgs
Summary:
Gets or sets the character corresponding to the key pressed.

Return Values:
The ASCII character that is composed. For example, if the user presses SHIFT
+ K, this property returns an uppercase K.
 
A

Armin Zingler

John Brown said:
Private Sub TextBox1_KeyPress( _

Doesn't "ByVal" indicate that "e" is read only?
(If I change it to "ByRef" I get more errors.)


You wrote that "KeyChar" is readonly. "e" is not KeyChar, and it is not a
property but an argument. My code does not change "e" but e.Keychar.
Anyways, the terms ByVal/ByRef and ReadOnly have no relation. ByVal/ByRef
specify how the value is passed to the procedure.

See also (bottom paragraphs):
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/366a2d399fc447a6

And [F1], as always. ;-)


Did you try my suggestion "go to definition"...?


Armin
 
A

Armin Zingler

John Brown said:
Armin,

I think I see something after following your instructions:
For:
KeyPressEventArgs[Compact Framework]

System.Windows.Forms.KeyPressEventArgs.KeyChar

Public ReadOnly Property KeyChar() As Char
Member of: System.Windows.Forms.KeyPressEventArgs
Summary:
Gets or sets the character corresponding to the key pressed.

Return Values:
The ASCII character that is composed. For example, if the user
presses SHIFT + K, this property returns an uppercase K.


I guessed you are using the Compact Framework but I didn't find any hint
that the property is readonly in the CF. Obviously it is. I'm afraid, I
don't know if and how it is possible to do what you're trying. I think it's
not possible.


Armin
 
G

Guest

I do appreciate your help and advice.

Thanks,
--
John Brown


Armin Zingler said:
John Brown said:
Armin,

I think I see something after following your instructions:
For:
KeyPressEventArgs[Compact Framework]

System.Windows.Forms.KeyPressEventArgs.KeyChar

Public ReadOnly Property KeyChar() As Char
Member of: System.Windows.Forms.KeyPressEventArgs
Summary:
Gets or sets the character corresponding to the key pressed.

Return Values:
The ASCII character that is composed. For example, if the user
presses SHIFT + K, this property returns an uppercase K.


I guessed you are using the Compact Framework but I didn't find any hint
that the property is readonly in the CF. Obviously it is. I'm afraid, I
don't know if and how it is possible to do what you're trying. I think it's
not possible.


Armin
 
G

Guest

SOLVED - A local programmer told me the solution. It is:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case e.KeyChar
Case "U"c, "u"c
Me.TextBox1.Text = Me.TextBox1.Text & "1"
e.Handled = True
Case Else
'at this point accept other keystrokes
End Select
End Sub

My only problem at this point is to get the insertion point / cursor back to
the end of the text in the text box. This code puts it at the begenning.
 
L

Lloyd Sheen

John Brown said:
SOLVED - A local programmer told me the solution. It is:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case e.KeyChar
Case "U"c, "u"c
Me.TextBox1.Text = Me.TextBox1.Text & "1"
e.Handled = True
Case Else
'at this point accept other keystrokes
End Select
End Sub

My only problem at this point is to get the insertion point / cursor back
to
the end of the text in the text box. This code puts it at the begenning.

That is not your only problem since the solution above assumes that the
keystroke is when the cursor is at the end of existing text within the
textbox. You need to find out where the character would be inserted and
process accordingly.

Hope this helps
Lloyd Sheen
 
C

Cor Ligthert[MVP]

John,

You got so many answers, I did not all evaluate them, however when I now see
the answer, I got the idea that classic VB programmers want forever to use
the KeyPress.

Why not the KeyUp that gives much more information?

If the keyboard is not dammaged, you can be sure that a key that is pressed
will be up before the event is handled.

Just as addition.

Cor
 
C

Cor Ligthert [MVP]

Jack,

Confirm that, you get back the Unicode key notation that is composed, which
can be a single key or a composed code.

Case U, u

For me a little bit less work than finding out which keys were pressed
before. I use it always.

Cor
 
J

Jack Jackson

I was talking about holding down a key so that it repeats. Not sure
what your comment means.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top