Capture Keys in Textbox, VB.net 2005:

I'm working on learning VB.Net, using 2005,

In VB 6, when you use a textbox:

Private Sub txtPv_KeyPress(Index As Integer, KeyAscii As Integer)
'If the ENTER is pressed, then call the function to set the PV.
If KeyAscii = 13 Then
Call cmdSetPv_Click(Index)
End If

End Sub

However, with VB.Net, when I select the "KeyPress" I get:

PrivateSub TextBox1_KeyPress(ByVal senderAsObject,ByVal eAs System.Windows.Forms.KeyPressEventArgs)Handles TextBox1.KeyPress

EndSub

I can't figure out how to capture the keys that were pressed using the arguments to the TextBox1_KeyPress Sub.

All of the help that I can find tells me that the "KeyPress" is as:

PrivateSub Text1_KeyPress(KeyAsciiAsInteger)

What am I missing?

Mfroster

[2222 byte] By [mfroster] at [2007-12-22]
# 1

I think its pretty similar to you vb6 process, although i use Keydown, intead of Keypress (someone who knows could explain why you would use one instead of the other)

I think the keypress event uses e.keychar instead of e.keyvalue

Ie

Private Sub FrmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

If e.KeyValue = Keys.Tab Then

...

Else

....

End If

End Sub

Or you could use a select case statement similar to

Select Case e.keyvalue

Case 9 ' 9 = Tab

...

Case 13 ' 13 = Enter

...

End Select

Hope That helps you somehow :)

McWhirter at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Hi,

Hope this help. I think you have missing out some format. Just grap this in and try :)

Private Sub txtbox_keypress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress, TextBox7.KeyPress, TextBox8.KeyPress

If e.KeyChar = Chr(13) Then 'should add in Tab

'chr(8) is backspace

'chr(9) is TAB

'chr(13) is Return

End If

End Sub

jacobtan at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

A way to make this code more readable is to use the Keys enumeration directly. You can call Microsoft.VisualBasic.ChrW() on Keys enumeration. For example

if e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then

JaredParsonsMSFT at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Thanks guys, it works.

I had tried the "e.keychar" but apparently not correctly.

I really appreciate all the hints.
On to the next learning experience...

Mfroster

mfroster at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Agree with this :)
weirdbeardmt at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

I have found that using a chr() works with e.keychar. for instance:

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

If e.KeyChar = Chr(65) then 'Chr(65) = A

lblLetterDisplay.text = A

else

bah

end if

end sub

RDT at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

I'm new in programming with vb.net 2005 and I hope u can help me with this

how can I use this samples so I can start loading a combobox, for instance: using a textbox - I press 'm' in my keyboard and load in a combobox all items that start with 'm'.

NnethKe at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...