Results 1 to 7 of 7

Thread: [RESOLVED] [2008] Multiline Textbox and KeyDown Event

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [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!
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Multiline Textbox and KeyDown Event

    Works fine for me. That said, your code should be:
    vb.net Code:
    1. If e.KeyCode = Keys.Right OrElse e.KeyCode = Keys.Down Then TextBox2.Select()
    or:
    vb.net Code:
    1. Select Case e.KeyCode
    2.     Case Keys.Right, Keys.Down
    3.         TextBox2.Select()
    4. End Select
    You should not have two separate If blocks and you should not be calling Focus.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Select Case e.KeyCode
    2.     Case Keys.Right, Keys.Down
    3.         TextBox2.Select()
    4.         e.SuppressKeyPress = True
    5. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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."
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width