Link to home
Start Free TrialLog in
Avatar of mgmhicks
mgmhicks

asked on

How to determine last textbox with focus.

I have a form that looks like a keyboard, on the form is about 3 panels.  Depending on what the user is doing determines what panel is visible and then, what textboxes are available.  The idea being hit clicks on name text field lets say, and then hits the a, b,c, etc... on the keyboard.  I need to know what field he just came from so I know what fields to put the keyboard items into.  I hope that makes sense.

thanks
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

You can use

Screen.PreviousControl.Name

mx
Avatar of mgmhicks
mgmhicks

ASKER

receiving PreviousControl.name is not a member of screen.
But it is ... seem image

something else is up

mx
Capture1.gif

I don't think he is in Access.  I believe he is in VB.NET

Regards,

Keith
mgmhicks,
What platform are you in?

mx
User generated image
Sorry this is what I see.  Using VS 2008
You are not in Access.  I would move this Question to the VB.Net / VB zone.

I don't know the answer ... sorry.

mx
Use a COMMON handler for ALL of your TextBoxes by listing them all after the "Handles" keyword.

Something like:
Public Class Form1

    Private LastTextBox As TextBox = Nothing

    Private Sub TB_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
        LastTextBox = DirectCast(sender, TextBox)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not IsNothing(LastTextBox) Then
            LastTextBox.SelectedText = Button1.Text
        End If
    End Sub

End Class

Open in new window


Note that if your buttons have the Text to be inserted as the caption of the button, AND all of the buttons do the exact same thing, then you can make all of them fire the same handler in the same way as the TextBoxes above:
Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        If Not IsNothing(LastTextBox) Then
            Dim btn As Button = DirectCast(sender, Button)
            LastTextBox.SelectedText = btn.Text
        End If
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bobHacker
bobHacker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ended up using lost focus event and using the tag property of the form to know what field to change.

thanks