Thebrakshow
Thebrakshow

Reputation: 23

Adding text to a textbox depending where the Cursor is

Let me start off with saying I have looked around and I guess my question is too specific to get any straight forward results. Also this is my first VB project.

I am trying to make a simple point of sale GUI. I have two textboxes (Price, Cash tendered) and 11 buttons making a number keypad. What I want to do is be able to click on the number keypad buttons (for example, buttons "1" and "0" to enter 10 dollars) and it go to the textbox that the cursor last clicked on.

So here is an example: I click on the textbox "Price" then click the button "1" and the button "0". This will then type 10 to the "Price" textbox. After that, I want to be able to click on the "Cash Tendered" textbox and be able to use the same number keypad buttons as before for the same operation.

A picture of the GUI is available.

Upvotes: 2

Views: 556

Answers (1)

David Baker
David Baker

Reputation: 94

You could use a boolean to keep track of which textbox was last selected

When you click in the Price textbox set the boolean as true. When you click in the Cash Tendered textbox set the boolean as false. When you click one of the number buttons in the keypad, test the boolean value to see which textbox was last selected, then enter the numbers into that textbox.

Example:

Public Class Form1

    Dim lastClickedTextBox1 As Boolean

    Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
        lastClickedTextBox1 = True
    End Sub

    Private Sub TextBox2_Click(sender As Object, e As EventArgs) Handles TextBox2.Click
        lastClickedTextBox1 = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If lastClickedTextBox1 = True Then
            TextBox1.Text = "Box 1 was it"
        Else
            TextBox2.Text = "Box 2 was it"
        End If
    End Sub
End Class

Upvotes: 2

Related Questions