VB2010- Input values to several textboxes using a visual created keypad

답변됨 VB2010- Input values to several textboxes using a visual created keypad

  • Thursday, April 12, 2012 12:15 PM
     
      Has Code

    Hi

    I have some problems with coding my visual keypad in order to input a value when the selected textbox has focus.With the code below makes the value I chose in all the textboxes.

    Here the code:

        'Number pad section------------------------------------------------
        'Number 1
        Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
            Dim cControln As Control
            For Each cControln In Me.Controls
                'Look for text boxes only
                If TypeName(cControln) = "TextBox" And cControln.Focus() Then
                    cControln.Text = cControln.Text & 1
                End If
                ' The Next textbox to select
            Next cControln
        End Sub

    Here the designer:

All Replies

  • Thursday, April 12, 2012 12:28 PM
     
     
    You'd want to use the .Focused property, not .Focus()

    Please call me Frank :)

  • Thursday, April 12, 2012 12:32 PM
     
     Answered Has Code

    You can't have a selected textbox and also click a button.  You can have a most recently selected textbox - is that what you mean?

    If so, create a form-level variable that tracks the most recently selected textbox.  Use that variable for the updating.

        Dim CurrentTextBox As TextBox
        Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
                TextBox1.Leave, _
                TextBox2.Leave, _
                TextBox3.Leave, _
                TextBox4.Leave, _
                TextBox5.Leave
            CurrentTextBox = CType(sender, TextBox)
        End Sub
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            CurrentTextBox.Text = CurrentTextBox.Text & "1"
        End Sub
    • Marked As Answer by AdilX Thursday, April 12, 2012 12:42 PM
    •  
  • Thursday, April 12, 2012 12:37 PM
     
      Has Code

    My program start with the tab stop active on the "a" using the keypad the user has to input a number then with the "->" tab it will go to the next one and so one.I just want the keypad to work in the textbox focused.

    To Frank: with the Focused it doesn't work.

    To Acamar : Thanks you very much solved my problem.

    Can I post another question related to the keypad without open an new thread?

    If I can, then I have some problem with the Backspace button, below is  the code,I can't make it work on the focused textbox,it just work on all of them, do I have to use again the CurrentTextBox?

    Private Sub Button14_Click(sender As System.Object, e As System.EventArgs) Handles Button14.Click
            If TextBox1.Text < " " Then
                TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1 + 1)
            Else
                TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
            End If
            If TextBox2.Text < " " Then
                TextBox2.Text = Mid(TextBox2.Text, 1, Len(TextBox2.Text) - 1 + 1)
            Else
                TextBox2.Text = Mid(TextBox2.Text, 1, Len(TextBox2.Text) - 1)
            End If



    • Edited by AdilX Thursday, April 12, 2012 12:41 PM
    • Edited by AdilX Thursday, April 12, 2012 12:43 PM
    • Edited by AdilX Thursday, April 12, 2012 12:59 PM
    • Edited by AdilX Thursday, April 12, 2012 1:00 PM
    • Edited by AdilX Thursday, April 12, 2012 1:02 PM
    •  
  • Friday, April 13, 2012 11:07 AM
     
     Answered

    If I can, then I have some problem with the Backspace button, below is  the code,I can't make it work on the focused textbox,it just work on all of them, do I have to use again the CurrentTextBox?

    Yes.  Use it for all the operations on the text of a textbox, unless that operation is only relevant for one specific text box, or for something such as Clear which you might apply to all text boxes.

    • Marked As Answer by AdilX Thursday, April 19, 2012 8:47 AM
    •