Find out which control had focus

Hi all

I have a series of text boxes in VB .NET 2008 Express
Named txtCmd01 through to txtCmd20

I have one cmdBrowse button.

When a text box get's focus, the browse button moves to the same .Top property as the textbox - this looks a lot better than having 20 cmd buttons.
The cmd button will open a dialog box to select a file. The filename has to be returned the textbox that had the focus however.

Do you know how I can do this?

Thanks
LVL 15
Ryan_RIT Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anyoneisSoftware DeveloperCommented:
When you get focus, you move the command button, so why not go ahead and store off the control id of the textbox. Put it in a global, or perhaps use the command button's Tag property.

With that, the button has no problem figuring out where to put the filename.

David
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adriankohwsCommented:
   me.ActiveControl.Name.ToString
0
adriankohwsCommented:
Alternatively, you can do something like this.


    Private Sub txtCmd02_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCmd01.GotFocus, txtCmd02.GotFocus
        'If all are textboxes
        'This example shows you only two textboxes
        Dim TB As TextBox = sender
        Select Case TB.Name
            Case "txtCmd01"
                'Do here
            Case "txtCmd02"
                'Do here
        End Select
    End Sub
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Ryan_RIT Systems AdministratorAuthor Commented:
Ok. Can you clarify what commands to use?
cmdBrowse.Tag = ???
 
 
???.Text = opnFile.Filename

Open in new window

0
anyoneisSoftware DeveloperCommented:
The Tag is property is a reference to an object, so it can hold anything,

//TextBox GotFocus event
...
cmdBrowse.Tag =  sender;
...
//

// In the Button Click handler, after return from dialog

Control theButton = sender as Button
TextBox theTextBoxPartner = theButton.Tag as TextBox;
theTextBoxPartner.Text = theFileNameText;

//

DAvid

0
taskyCommented:
You can declare a private reference to the textbox within the form

Dim txtSel As TextBox

Then when a textbox gets focus, set the reference of txtSel to the textbox. I.e.:

Private Sub txtCmd02_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCmd01.GotFocus, txtCmd02.GotFocus

txtSel = sender

End Sub

Then when you click the command button, just use txtSel and it will point to the last selected textbox.
0
Ryan_RIT Systems AdministratorAuthor Commented:
I had started off using the global variable option, then had doubts, asked this question, then realised my initial method would work.

I'll split the points anyway.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.