Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Get the name of Clicked Control


Reply
 
Thread Tools Display Modes
  #1  
Old 06-26-2012, 07:22 PM
skyfox's Avatar
skyfox skyfox is offline
Centurion
 
Join Date: Feb 2005
Location: InsideACircularReference
Posts: 171
Default 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"
Reply With Quote
  #2  
Old 06-26-2012, 07:54 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

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.
Reply With Quote
  #3  
Old 06-27-2012, 08:42 AM
skyfox's Avatar
skyfox skyfox is offline
Centurion
 
Join Date: Feb 2005
Location: InsideACircularReference
Posts: 171
Default

Quote:
Originally Posted by passel View Post
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.
Reply With Quote
  #4  
Old 06-27-2012, 12:47 PM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is online now
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,734
Default

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
Reply With Quote
  #5  
Old 06-27-2012, 05:00 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,877
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->