locked
up and down arrows keypresses in a textbox

    Question

  • when i press the up/down arrows whilst in a textbox it moves the cursor left and right
    i dont want it to do this but would like the left/right arrows to till move the cursor
    is there any way to stop it doing this?
    i tried storing the cursor position on key down and then resetting it but the cursor visibly moves to its new position then back again which isnt great

    thanks
    Sunday, May 03, 2009 5:46 PM

Answers

  • thanks i put this code in the keypress event and the cursor doesnt jump anymore


    That is strange because the Up and Down keys do not raise the KeyPress event in a TextBox.

    Try this instead:

        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
                e.SuppressKeyPress = True
            End If
        End Sub
    Sunday, May 03, 2009 10:00 PM

All replies

  • Handle the KeyDown event and if the current key is left or right then place the cursor back to the original position.
    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    • Edited by A.m.a.L Hashim Sunday, May 03, 2009 6:07 PM edit
    • Marked as answer by Seekaye Sunday, May 03, 2009 6:22 PM
    • Unmarked as answer by Seekaye Sunday, May 03, 2009 6:46 PM
    Sunday, May 03, 2009 6:07 PM
  • thanks i put this code in the keypress event and the cursor doesnt jump anymore




    ' if up

    If
    e.KeyCode = 38 Then

     

    If TextBox1.SelectionStart <> Len(TextBox1.Text) Then

    TextBox1.SelectionStart = TextBox1.SelectionStart + 1

     

    End If

     

    ' If down Then

     

    If e.KeyCode = 40 Then

     

    If TextBox1.SelectionStart <> 0 Then

    TextBox1.SelectionStart = TextBox1.SelectionStart - 1

     

    End If

     

    Sunday, May 03, 2009 6:19 PM
  • Nice to hear your issue got resolved :)
    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Sunday, May 03, 2009 6:21 PM
  • actually i just realised this doesnt work when the cursor is at the start as you cant move the cursor one place to the left then. similarly when its at the far end
    any ideas welcome
    Sunday, May 03, 2009 6:46 PM
  • check where the position is.

    If postion = 0 then dont move to left.

    If position is equal to textbox.Text.Length then dont move right.

    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Sunday, May 03, 2009 7:02 PM
  • pressing the arrow moves the cursor
    my code above moves the cursor which is then undone by the arrow press
    if i cant move the cursor left (because its at the start) or right (because its at the end) then the cursor ends up in the wrong place after the arrow press

    i tried storing the cursor position in a global variable on keydown and then resetting it on keyup. this works but the cursor visibly moves

    Sunday, May 03, 2009 7:41 PM
  • Please post the current code you are working on.

    One question have you tried the logic i have explained above?
    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Sunday, May 03, 2009 7:54 PM
  • thanks i put this code in the keypress event and the cursor doesnt jump anymore


    That is strange because the Up and Down keys do not raise the KeyPress event in a TextBox.

    Try this instead:

        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
                e.SuppressKeyPress = True
            End If
        End Sub
    Sunday, May 03, 2009 10:00 PM
  • sorry dave i meant the keydown event
    anyways this e.supresskeypress = true is the exact code i'm after
    many thanks
    Monday, May 04, 2009 8:34 AM