Textbox KeyDown Overrides
-
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
-
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
-
tried it already. those take up more space than i have.
-
tried it already. those take up more space than i have.
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
-
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
ah. i missed the e.handled = true part.