Search code examples
vb.nettextarrow-keys

VB.net move between textboxes by keyboard arrow


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


Solution

  • 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