PDA

View Full Version : Solved: Which control had focus?



andy_uk
08-10-2004, 05:32 PM
Hi all, help please.

I have a UserForm with loads of controls (various types) on 2 tabs. I need a cmd_Click that will ID which control had focus last.

I've established that I need to set the cmd TakeFocusOnClick to False, but "Range(whatever) = ActiveControl.Name" only gives me "MultiPage1", like it's in front or something. Any suggestions?

TIA,
Andy

Jacob Hilderbrand
08-10-2004, 06:50 PM
Multipage will be the active control regardless of what controls on the multipage are active. You can try to setup a global variable that can track the current control. Set the Enter sub for each control to change the variable to the control.


Option Explicit

Dim Ctl As Control

Private Sub CommandButton1_Enter()

Set Ctl = UserForm1.CommandButton1

End Sub

Private Sub CommandButton2_Enter()

Set Ctl = UserForm1.CommandButton2

End Sub

Private Sub CommandButton5_Enter()

Set Ctl = UserForm1.CommandButton5

End Sub

Daniel Klann
08-10-2004, 07:44 PM
Hi,

You can get the active control on a Multipage by using something like this:-

MsgBox ActiveControl.SelectedItem.ActiveControl.Name


HTH
Dan

Zack Barresse
08-10-2004, 07:59 PM
Hey Dan, nice one. I have a question regarding the syntax of that one: why designate the object twice? I haven't worked with controls much, but that is just bizarre. While quite short, is there a way to bypass referencing the object multiple times for one call? Maybe you could set me straight on this one. ;)

jamescol
08-10-2004, 07:59 PM
You may need to modify Jacob's code to keep track of the last control to have focus. Otherwise I think you will end up with the current control.


Option Explicit

Dim PrevCtl As Control

Private Sub CommandButton1_Exit()

Set PrevCtl = UserForm1.CommandButton1

End Sub

Private Sub CommandButton2_Exit()

Set PrevCtl = UserForm1.CommandButton2

End Sub

Private Sub CommandButton5_Exit()

Set PrevCtl = UserForm1.CommandButton5

End Sub


If you are planning to do anything other than display the control that last had focus, you will need additional logic to switch between multipage tabs. For instance, if page 1 is displayed, you can't set the focus to a control on page 0 with first changing to that page.

Also, you will need to set the PrevCtl during the Enter event of the default control that has focus when you first load the form or your button won't work properly. You could also just set the PrevCtl during the Enter event, but to me the logic isn't as intuitive.

My .02

James

Jacob Hilderbrand
08-10-2004, 08:10 PM
I don't think we need to use the exit Exit sub since the command button being used will not take the focus. So the previous value we set in the Enter sub will still be in effect.

jamescol
08-10-2004, 08:16 PM
Jacob,
You may be right. I was understanding the problem statement that he wants to click a button and diplay the previous control (not counting the button). So basically an N-1 deal.

You interpretation is that he wants the button to display the control that had the focus just before the button.

Not sure which scenario is the one he wants. Guess we got both covered :)

Cheers,
James

andy_uk
08-11-2004, 12:33 AM
Actually, Daniel Klann's "ActiveControl.SelectedItem.ActiveControl.Name" works perfectly here (sorry, XL2K on XP Home, not used to asking questions).

firefytr - if I crop it to "ActiveControl.SelectedItem.Name" it gives me the name of the MultiPage, which makes sense within the whole layer/object hierarchy thing (multipage owns each item on it).

Daniel, I googled for "ActiveControl.SelectedItem.ActiveControl.Name" and got one solitary hit, so all I can say is if it works for the user then very well done.

:thumb

TVM,
Andy

Daniel Klann
08-11-2004, 04:15 AM
Hey Dan, nice one. I have a question regarding the syntax of that one: why designate the object twice? I haven't worked with controls much, but that is just bizarre. While quite short, is there a way to bypass referencing the object multiple times for one call? Maybe you could set me straight on this one. ;)Hi Zack,

If you break it down it's not that bizarre:-

If the form contains a Multipage control the Msgbox.ActiveControl.Name would return the name of the Multipage control e.g. Multipage1.
The Multipage control has a SelectedItem property, which returns the currently selected tab or page. BTW, I found this by looking in the object browser at the available properties and checking help to see what each one did. That is one of the best way IMO to find answers to these sort of questions.
The page object acts as a container for controls and has some of the same properties and methods as your bog standard userform e.g. a Controls collection and an ActiveControl property (which is what we need).

You could use more verbose code such as this:-


Private Sub CommandButton1_Click()
Dim Mp As MSForms.MultiPage
Dim Pg As MSForms.Page
Dim Ct As MSForms.Control

'This will give a reference to a multipage contro
Set Mp = ActiveControl
'This will give a reference to selected page on the
'multipage control
Set Pg = Mp.SelectedItem

'Now get a reference to the active control on the
'selected page
Set Ct = Pg.ActiveControl

MsgBox "ActiveControl = " & Mp.Name & vbCr & _
"Page Number = " & Pg.Name & vbCr & _
"Page.ActiveControl = " & Ct.Name
End Sub


Does that make sense? ;)

Dan

Zack Barresse
08-11-2004, 10:23 AM
Thanks for that Dan! I appreciate you taking the time to explain it to me. I'm always learning and always enjoy a lesson from one as astute as yourself!

Take care! :)