increasing a text box value

  • Thread starter Thread starter Alfred
  • Start date Start date
A

Alfred

Hi
I am trying to change over from access to vb.net. If I want to increase a
value I used the following in Access. How can I change it to vb.net.
Example if I press the up arrow the value in the control must increase by
one.

Public Sub UpDownKey(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyUp
KeyCode = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case vbKeyDown ' Minus key
KeyCode = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub

Thanks for help
Alfred
 
Alfred said:
Hi
I am trying to change over from access to vb.net. If I want to increase a
value I used the following in Access. How can I change it to vb.net.
Example if I press the up arrow the value in the control must increase by
one.

Public Sub UpDownKey(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyUp
KeyCode = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case vbKeyDown ' Minus key
KeyCode = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub


First of all, make sure the KeyPreview property on your form is True,
otherwise it won't get the key events.

Then:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If Not ActiveControl Is Nothing AndAlso TypeOf ActiveControl Is TextBox
Then
Dim txt As TextBox = CType(ActiveControl, TextBox)
Select Case e.KeyCode
Case Key.Up
txt.Text = (Val(txt.Text) + 1).ToString()
Case Key.Down
txt.Text = (Val(txt.Text) - 1).ToString()
End Select
End If
End Sub

That should do the trick.

(note: Although you can use Object and late binding to make this more
general than just for TextBox, VB.NET doesn't have default properties, so
you *have* to specify the property to change)
 
Back
Top