Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Textbox KeyDown Overrides

Textbox KeyDown Overrides

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    medicenpringles
    wrote on last edited by
    #1

    I have a textbox that the user inputs numbers into. i would like to be able to create a KeyDown event for the control that, when the user presses the UP arrow, the value raises by one. this i have accomplished. but unfortunately this does not override the action already taken (moving the cursor to the left). how to i override this? thanks ahead of time, stephen

    A 1 Reply Last reply
    0
    • M medicenpringles

      I have a textbox that the user inputs numbers into. i would like to be able to create a KeyDown event for the control that, when the user presses the UP arrow, the value raises by one. this i have accomplished. but unfortunately this does not override the action already taken (moving the cursor to the left). how to i override this? thanks ahead of time, stephen

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      Sounds like you could use a numericUpDown control instead.

      M 1 Reply Last reply
      0
      • A Anonymous

        Sounds like you could use a numericUpDown control instead.

        M Offline
        M Offline
        medicenpringles
        wrote on last edited by
        #3

        tried it already. those take up more space than i have.

        A 1 Reply Last reply
        0
        • M medicenpringles

          tried it already. those take up more space than i have.

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Hmm, well you intercept the key, increase the value, and say event handled. The caret won't move as you haven't asked it to. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If (e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down) AndAlso IsNumeric(Me.TextBox1.Text) Then Dim i As Integer = CInt(Me.TextBox1.Text) 'check key again, if it is up add 1, otherwise add -1 i += IIf(e.KeyCode = Keys.Up, 1, -1) Me.TextBox1.Text = i.ToString 'start text selection at end of text, length 0 to move caret Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length Me.TextBox1.SelectionLength = 0 e.Handled = True End If End Sub

          M 1 Reply Last reply
          0
          • A Anonymous

            Hmm, well you intercept the key, increase the value, and say event handled. The caret won't move as you haven't asked it to. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If (e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down) AndAlso IsNumeric(Me.TextBox1.Text) Then Dim i As Integer = CInt(Me.TextBox1.Text) 'check key again, if it is up add 1, otherwise add -1 i += IIf(e.KeyCode = Keys.Up, 1, -1) Me.TextBox1.Text = i.ToString 'start text selection at end of text, length 0 to move caret Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length Me.TextBox1.SelectionLength = 0 e.Handled = True End If End Sub

            M Offline
            M Offline
            medicenpringles
            wrote on last edited by
            #5

            ah. i missed the e.handled = true part.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups