Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 3rd, 2004, 07:15 AM
Authorized User
 
Join Date: Apr 2004
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help me please !

Could you tell me How to Handle keystroke in VB Net
For example in VB6 I write the code as

private sub textbox1_keypress(keycode as interger)
 if keycode=13 then TEXTBOX2.setfocus

end sub
but in vb Net I'm unable to know how to do

 
Old November 4th, 2004, 05:35 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Dead easy:

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

     ' enter handling code here, using members of e object...

End Sub
[code]

You can copy and paste code, but to get the development environment to show it automatically:
1) When you are looking at the code for your form, look at the top of the screen.
2) After the menu bar and the tool bar, there are a load of tabs, one for each code/designer window you have open.
3) Just under these tabs are two combo boxes - the one on the left will have something like 'Form1' in it, the one on the right will have something like '(Declarations)' in it.
4) If you drop-down the box on the left, you'll see that all the controls you've added to the form are listed. Select the control you want to add an event handler for.
5) Then, drop down the right-hand combo. This will list all the events for the selected object. Events which already have handlers are listed in bold

Advanced:

If you want a method to handle events for two or more controls, create the handler as I've described above. You'll notice that after the method declaration there's a handles keyword:
Code:
Private Sub OnKeyPress(sender as Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If you put a comma, you can then type the name of a new [Control].[Event] for the routine to handle (intellisense will help you with this), i.e.
Code:
Private Sub OnKeyPress(sender as Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress
Hope this sorts you out

 
Old November 5th, 2004, 05:45 AM
Authorized User
 
Join Date: Apr 2004
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes I know ! But what i want is :
When I hit the Enter key the cursor move to the Next textbox on the form
I dont want hit the Tab key
do u understand ?

 
Old November 5th, 2004, 08:36 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Quote:
quote:Originally posted by minhtri
 Yes I know ! But what i want is :
When I hit the Enter key the cursor move to the Next textbox on the form
I dont want hit the Tab key
do u understand ?
You're not very clear.

What are you trying to achieve with this? If you are trying to let the user put tabs into a textbox, then just set the 'AcceptsTab' property of the relevant TextBox control to True.
This will also mean (I believe) that you can catch the tab key in the KeyDown/KeyPress events and cancel it.

if you want to cancel the tab keystroke for a particular control (i.e. TextBox1) when 'AcceptsTab' is set to False, then try the following:

Code:
    Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
        If Me.ActiveControl.Name = "TextBox1" Then
            forward = False
        End If
    End Function
 
Old November 5th, 2004, 09:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

I believe what is wanted is to substitute the enter key for the tab key, so that when the user is entering data, they use the enter key to move to the next field.

Code in the KeyDown event, not KeyPress:

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{Tab}")
        End If
End Sub

Note that a simple search on Google would have turned this up.

J









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.