PDA

View Full Version : Control TypeName



vpager
10-30-2008, 10:00 AM
Best to explain my question with examples:

Place a listbox on a userform. Add the following code to the userform to add some entries to the listbox and set a click event for the listbox to display a message with the TypeName of the clicked control.

Private Sub UserForm_Initialize()
For r = 1 To 6
ListBox1.AddItem r
Next r
End Sub

Private Sub ListBox1_Click()
MsgBox (TypeName(UserForm1.ActiveControl))
End Sub


Display the userform.
Click an entry in the listbox and message displays with "ListBox"
So far so good.

Second example:
Place a multipage on a userform.
Add a listbox to the multipage.

Add the same code as above.
Display the userform.
Click an entry in the listbox and the message displays "MultiPage" instead of "ListBox"

The same thing happens with any control object placed within the multipage.

But I am trying to identify when someone has clicked on a listbox control.

Any suggestions?

Before you post:
1. This is for the purpose of adding mouse scrolling to the listbox. So a listbox event is not available.
2. The listbox must remain on the multipage.


Thanks
Mike

Kenneth Hobs
10-30-2008, 11:29 AM
A google search would find a solution. Not sure why you don't just use ListBox1 since you know it. The typical method might be:
MsgBox MultiPage1.Pages(MultiPage1.Value).ActiveControl.Name
A more detailed method could be:
Private Sub ListBox1_Click()
'MsgBox MultiPage1.Pages(MultiPage1.Value).ActiveControl.Name
'MsgBox (TypeName(UserForm1.ActiveControl))
ShowTypeName ActiveControl
End Sub

Private Sub ShowTypeName(c As MSForms.Control)
Dim s As String
If c.Name = "MultiPage1" Then
s = c.Pages(c.Value).ActiveControl.Name
Else: s = c.Name
End If
MsgBox s
End Sub
For a more robust method:
Private Sub ListBox1_Click()
ShowTypeName2
End Sub

Private Sub ShowTypeName2()
Dim s As String, c As Control
Set c = ActiveControl
If TypeName(c) = "MultiPage" Then
s = c.Pages(c.Value).ActiveControl.Name
Else: s = c.Name
End If
MsgBox s
End Sub

vpager
10-31-2008, 03:23 AM
Hi Kenneth

Thanks for your suggestions.
I used the Click event just to give examples of my problem. The actual code uses user32.dll functions and WindowProc to capture mouse wheel movements, which VBA doesn't have a direct means of handling (unless you can tell me otherwise).

When the mouse wheel moves, the code needs to identify the type of the active control and only do something if it's a listbox.

Your ShowTypeName examples display the name of the control but not the control type; eg "ListBox".

It may still give me enough of a direction to find the answer I'm looking for.

Thanks
Mike

vpager
10-31-2008, 07:06 AM
The following works perfectly.

Set ac = ActiveControl
If TypeName(ac) = "MultiPage" Then Set ac = ac.Pages(ac.SelectedItem.Index).ActiveControl
If TypeName(ac) = "ListBox" Then




Thanks