Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.




1617 posts

Uber Geek
+1 received by user: 729


Topic # 89797 8-Sep-2011 09:12
Send private message

Bit of a long shot here, I’m stuck in the dark ages using Borland C++ Builder v6.

I have multiple components on my Form (all of the same type, dynamically created in this case) using the same OnClick event handler.

Stepping through the code, the event handler fires OK.  Within the event handler function, I need to determine which one of the components triggered it.  Is there any way to do this?  I know that the function is called with a parameter of TObject* Sender, but ‘Inspect’ing this doesn’t seem to give any useful info.

(I should also point out that I'm a self-taught hobbyist programmer, so please bear with me if I ask any noob questions)




Don't use 'beefstew' as a password.  It's not stroganoff.


Create new topic
Will not stab you
226 posts

Master Geek
+1 received by user: 17

Subscriber

  Reply # 518451 8-Sep-2011 10:49
Send private message

Set the tag on each component to a different integer, when build a switch/case around the tag.




Recursion: See recursion.
--
“It is important not to let the perfect become the enemy of the good, even when you can agree on what perfect is. Doubly so when you can't. As unpleasant as it is to be trapped by past mistakes, you can't make any progress by being afraid of your own shadow during design.”

     --Greg Hudson, Subversion developer



1617 posts

Uber Geek
+1 received by user: 729


  Reply # 518455 8-Sep-2011 11:04
Send private message

BuffyNZ: Set the tag on each component to a different integer, when build a switch/case around the tag.


Thanks Buffy, I'll try that at home tonight.  Does that mean the tag property is accessible from the TObject and inherited all the way down the tree?




Don't use 'beefstew' as a password.  It's not stroganoff.


 
 
 
 


Try Wrike: fast, easy, and efficient project collaboration software
Will not stab you
226 posts

Master Geek
+1 received by user: 17

Subscriber

  Reply # 518460 8-Sep-2011 11:08
Send private message

It has been many many years, but it is either a TObject property or part of another common ancestor of all visual components.

If it is not a TObject, you could also just recast it to the common ancestor.

I can probably did some old code out of an archived source control backup if you get really stuck.

EDIT: It is Tag is a property of TComponent.

 




Recursion: See recursion.
--
“It is important not to let the perfect become the enemy of the good, even when you can agree on what perfect is. Doubly so when you can't. As unpleasant as it is to be trapped by past mistakes, you can't make any progress by being afraid of your own shadow during design.”

     --Greg Hudson, Subversion developer



1617 posts

Uber Geek
+1 received by user: 729


  Reply # 518571 8-Sep-2011 14:21
Send private message

Here comes the noob question. The 'recast' thing, would it go something like this

int x=((TComponent *)Sender)->Tag;

if I wanted to assign the value of Tag from the originating component to x within the event handler?




Don't use 'beefstew' as a password.  It's not stroganoff.


Will not stab you
226 posts

Master Geek
+1 received by user: 17

Subscriber

  Reply # 518584 8-Sep-2011 14:39
Send private message

That is the simplest, but not the safest or necessarily the 'best' way to do it. :)

Back when I was writing code for C++ builder I would have done exactly that.

I put best in quotes because its quite subjective, I am a pragmatic codes, if it works, and its simple, then its best for me. :)

The danger here is if sender is not a pointer to a TComponent for some reason.

If I was writing it now days I would probably do something like:

---
int tag = -1;

if (dynamic_cast < TComponent * > (Sender))
{
TComponent *comp = static_cast< TComponent * >Sender;
tag = comp->Tag;
}
---

The dynamic cast will return 0 if the object if not an instance of a pointer to TComponent

Of course you don't need to use just Tag, if there is some other identifier in the component, say the button text, you could just cast it to a TButton and pull the text, or the location, or the tab index..




Recursion: See recursion.
--
“It is important not to let the perfect become the enemy of the good, even when you can agree on what perfect is. Doubly so when you can't. As unpleasant as it is to be trapped by past mistakes, you can't make any progress by being afraid of your own shadow during design.”

     --Greg Hudson, Subversion developer



1617 posts

Uber Geek
+1 received by user: 729


  Reply # 518744 8-Sep-2011 19:54
Send private message

That worked a treat. It's been a very educational day. Thanks Buffy.




Don't use 'beefstew' as a password.  It's not stroganoff.


Create new topic



Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Opera launches new mobile browser: Opera Touch
Posted 25-Apr-2018 20:45


TCF and Telcos Toughen Up on Scam Callers
Posted 23-Apr-2018 09:39


Amazon launches the International Shopping Experience in the Amazon Shopping App
Posted 19-Apr-2018 08:38


Spark New Zealand and TVNZ to bring coverage of Rugby World Cup 2019
Posted 16-Apr-2018 06:55


How Google can seize Microsoft Office crown
Posted 14-Apr-2018 11:08


How back office transformation drives IRD efficiency
Posted 12-Apr-2018 21:15


iPod laws in a smartphone world: will we ever get copyright right?
Posted 12-Apr-2018 21:13


Lightbox service using big data and analytics to learn more about customers
Posted 9-Apr-2018 12:11


111 mobile caller location extended to iOS
Posted 6-Apr-2018 13:50


Huawei announces the HUAWEI P20 series
Posted 29-Mar-2018 11:41


Symantec Internet Security Threat Report shows increased endpoint technology risks
Posted 26-Mar-2018 18:29


Spark switches on long-range IoT network across New Zealand
Posted 26-Mar-2018 18:22


Stuff Pix enters streaming video market
Posted 21-Mar-2018 09:18


Windows no longer Microsoft’s main focus
Posted 13-Mar-2018 07:47


Why phone makers are obsessed with cameras
Posted 11-Mar-2018 12:25



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.