-1

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

user3077945
  • 13
  • 2
  • 9

2 Answers2

0

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
webdad3
  • 8,893
  • 30
  • 121
  • 223
0

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
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • thank you so much . Thats Worked Ok When Type .25 The Text Chang To 0.25 ... But When I Type 1 It Is Not Changed To 1.00 – user3077945 Dec 12 '13 at 15:10
  • You need to define the precision and scale of the decimal. Something like `Public Property Price as Decimal(10,2)` The first integer is the precision (the total number of digits allowed). The second integer is the scale (the number of digits allowed after the decimal point). – Jonathon Cowley-Thom Dec 12 '13 at 16:09
  • @Jonathon - This is not valid in .NET, you can't define a decimal precision. @user6077945 - with the above code it should change `1` to `1.00` double check your code – Jürgen Steinblock Dec 13 '13 at 06:42