VB.net move between textboxes by keyboard arrow

2.3k views Asked by At

I Have VB.net application form with 10 horizontal text boxes . I need to move between text boxes with right And Left Keyboard Arrow . Also I need Make textBoxes Format To Be Like This 0.00

2

There are 2 answers

3
Jürgen Steinblock On BEST ANSWER

In addition to webdad3's answer.

Private Sub Form1_KeyDown(ByVal sender as Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    Dim tb as TextBox = TryCast(me.ActiveControl, TextBox)
    If tb IsNot Nothing Then

        Select Vase e.KeyCode
            Vase Keys.Right, Keys.Left
                dim forward as boolean = e.KeyCode = Keys.Right
                me.SelectNextControl(Me, forward, True, True, true)            
                e.Handled = true
        End Select

    End If

End Sub

Don't forget to set Form.KeyPreview to true (via Forms Designer)

For the second part:

There are many, many different ways to format the text of a textbox. The best solution would be to use DataBindings (complex topic, read a book about it.)

Public Class Form1

    Public Property Price as Decimal

    ' call this code once
    Private Sub InitControls()

        Price = 3.45

        me.txtPrice.DataBindings.Add(
            "Text", Me, "Price", True, 
             DataSourceUpdateMode.OnPropertyChanged, Nothing, "0.00"
        )

    End Sub        

End Class
0
webdad3 On

I got the following code from the following link:

http://social.msdn.microsoft.com/Forums/windows/en-US/ffeeea42-f6ba-420f-827e-74879fd29b26/how-to- detect-arrow-keys-in-vbnet?forum=winforms

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    ' Sets Handled to true to prevent other controls from 
    ' receiving the key if an arrow key was pressed
    Dim bHandled As Boolean = False
    Select Case e.KeyCode
        Case Keys.Right
            'do stuff
            e.Handled = True
        Case Keys.Left
            'do other stuff
            e.Handled = True
        Case Keys.Up
            'do more stuff
            e.Handled = True
        Case Keys.Down
            'do more stuff
            e.Handled = True
    End Select
End Sub