increasing a text box value

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
 
S

Sven Groot

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top