Capture Keys in Textbox, VB.net 2005:  
Author Message
mfroster





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

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:

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

End Sub

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:

Private Sub Text1_KeyPress(KeyAscii As Integer)

What am I missing

Mfroster



Visual Basic14  
 
 
McWhirter





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

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 :)


 
 
jacob tan





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

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


 
 
Jared Parsons MSFT





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

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



 
 
mfroster





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

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

 
 
weirdbeardmt





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

Agree with this :)

 
 
RDT





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

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



 
 
NnethKe





PostPosted: Visual Basic General, Capture Keys in Textbox, VB.net 2005: Top

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'.