 |
 |

06-26-2012, 07:22 PM
|
 |
Centurion
|
|
Join Date: Feb 2005
Location: InsideACircularReference
Posts: 171
|
|
Get the name of Clicked Control
|
Is there a way to get the name of a clicked object or an object reference to the object clicked, from within same objects clicked event that can be passed to a Function or sub?
for an example......
Command1_Click()
dim a as string
dim b as object
a = objects name as Command1
set b = object refrence to Command1
' Then send a or b to an external function or sub.
End sub
What I am trying to accomplish is to get the OBJECT's name and a refrence to it without "EXPLICITLY" specifying it at design time, but within it's own click event.
Is this even Possible?
Thanks
|
__________________
SkyFox
Tinker of All, Master of NONE!
"To B or Not To VB? That's the Question"
|

06-26-2012, 07:54 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
Not sure why you would need to do this, but a quick look at the properties of a form in intellisense the first item on the Form's property list is "ActiveControl", which sounds like it could possibly be used.
Code:
Option Explicit
Dim ClickCount As Long
Private Sub Command1_Click()
Debug.Print Me.ActiveControl.Name
UpdateCaption Me.ActiveControl
End Sub
Private Sub Command2_Click()
Debug.Print Me.ActiveControl.Name
UpdateCaption Me.ActiveControl
End Sub
Private Sub UpdateCaption(c As Control)
ClickCount = ClickCount + 1
c.Caption = Str$(ClickCount)
End Sub
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

06-27-2012, 08:42 AM
|
 |
Centurion
|
|
Join Date: Feb 2005
Location: InsideACircularReference
Posts: 171
|
|
Quote:
Originally Posted by passel
Not sure why you would need to do this, but a quick look at the properties of a form in intellisense the first item on the Form's property list is "ActiveControl", which sounds like it could possibly be used.
|
Hi Passel,
Thanks fo the reply,
For the Why I need this.....
Basically I want to have a generic function where certain properties are set for a UserControl during run time. This Function performs certain actions on the object(s) passed to it based on type of UserControl, as well as determinning if the control is part of a control array or a singular control.
Single project (or a single form for that matter) can easily contain 100's of these usercontrols, where some of them only have a single boolean property while others contain both boolean and anlog properties. Each of these latter objects can have as much as 20 ~ 30 parameters (properties) per object. So I wanted to find a generic way to pass an object refrence to this function without having to explicitly code the object name for each instance of these aforementioned UserControls.
About the ActiveControl........
I forgot to mention (and using "Command1" in my original post was a bad exaple on my part), that my UserControl objects does not contain "Can Get Focus" property or "Set Focus" method, nor do I want them to have it. "ACTIVECONTROL" returns the object that currently has focus, and not necessarily the object that was clicked. But "ActiveControl" does what I need to do, but only if objects can get focus.
I was thinking along the line where if there was a way to detect the control under the mouse or to get it's info from it's own click event.
Thanks again for your help.
|
__________________
SkyFox
Tinker of All, Master of NONE!
"To B or Not To VB? That's the Question"
Last edited by skyfox; 06-27-2012 at 08:50 AM.
|

06-27-2012, 12:47 PM
|
 |
Multi-Technologist
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 3,734
|
|
|
2 possible options:
You can iterate through the controls collection looking for one whose coordinates are under the mouse.
Or, much more complicated, you can subclass for the click (WM_LBUTTONDOWN = &H201) and then perhaps get object from hWnd.
|
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
|

06-27-2012, 05:00 PM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,877
|
|
What your asking for is a bit odd ball. I know because I chased something like this before
Global Mouse Event for all controls Link
Finally I decided it just wasn't worth the effort.
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|