[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!
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.
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.
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.
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."
Re: [2008] Multiline Textbox and KeyDown Event
Quote:
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:
Quote:
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.
Quote:
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? ;)
Re: [RESOLVED] [2008] Multiline Textbox and KeyDown Event
Must be arriving any minute now