|
-
Sep 10th, 2008, 08:31 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2008] Multiline Textbox and KeyDown Event
Hi!!
I have two Multilined textboxes on my form (Textbox1 and Textbox2). Then I have this code:
Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Right Then TextBox2.Focus()
If e.KeyCode = Keys.Down Then TextBox2.Focus()
End Sub
I run the project I select the textbox1 and click the right arrow key, and he selects the textbox2 as it would be expected. Now the problem is: if I click in the down arrow key while selecting the textbox1, the textbox2 should get selected, but it doesnt. Why?
Thks a lot!
-
Sep 10th, 2008, 08:47 PM
#2
Re: [2008] Multiline Textbox and KeyDown Event
Works fine for me. That said, your code should be:
vb.net Code:
If e.KeyCode = Keys.Right OrElse e.KeyCode = Keys.Down Then TextBox2.Select()
or:
vb.net Code:
Select Case e.KeyCode Case Keys.Right, Keys.Down TextBox2.Select() End Select
You should not have two separate If blocks and you should not be calling Focus.
-
Sep 11th, 2008, 09:46 AM
#3
Thread Starter
Fanatic Member
Re: [2008] Multiline Textbox and KeyDown Event
Well It only works If I press right, because if I press down it doesnt work.
I've tried with a textbox where Multiline=False and it worked wonderfully. So I think its related to the multiline.
Last edited by Lasering; Sep 11th, 2008 at 01:47 PM.
-
Sep 11th, 2008, 04:50 PM
#4
Re: [2008] Multiline Textbox and KeyDown Event
I see what you mean. I overlooked the Multiline part. Sorry about that. Change the code to this and it will work:
vb.net Code:
Select Case e.KeyCode Case Keys.Right, Keys.Down TextBox2.Select() e.SuppressKeyPress = True End Select
Why exactly that should be, I don't know, because the arrow keys don't raise KeyPress events anyway. Seems like a bit of a bug but it's not worth worrying about with such a simple workaround.
-
Sep 11th, 2008, 05:34 PM
#5
Thread Starter
Fanatic Member
Re: [2008] Multiline Textbox and KeyDown Event
Code:
and you should not be calling Focus.
Just out of curiosity why should I call Select instead of Focus?
THKS a loot for the last code. Just a pro like you could know something like that, cause the solution "doesnt follow logic".
PS: I didnt rate you because I couldn't - "You must spread some Reputation around before giving it to jmcilhinney again."
-
Sep 11th, 2008, 06:25 PM
#6
Re: [2008] Multiline Textbox and KeyDown Event
 Originally Posted by Lasering
Just out of curiosity why should I call Select instead of Focus?
From the MSDN documentation for the Control.Focus method:
 Originally Posted by MSDN
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
 Originally Posted by Lasering
I didnt rate you because I couldn't - "You must spread some Reputation around before giving it to jmcilhinney again."
I don't do it for the reputation. I do it for the money. You did put a cheque in the mail, right?
-
Sep 11th, 2008, 07:30 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] [2008] Multiline Textbox and KeyDown Event
Must be arriving any minute now
Last edited by Lasering; Sep 11th, 2008 at 07:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|