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.


floydbloke

3522 posts

Uber Geek

ID Verified

#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)




Did Eric Clapton really think she looked wonderful...or was it after the 15th outfit she tried on and he just wanted to get to the party and get a drink?


Create new topic
BuffyNZ
241 posts

Master Geek


  #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




floydbloke

3522 posts

Uber Geek

ID Verified

  #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?




Did Eric Clapton really think she looked wonderful...or was it after the 15th outfit she tried on and he just wanted to get to the party and get a drink?


BuffyNZ
241 posts

Master Geek


  #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




floydbloke

3522 posts

Uber Geek

ID Verified

  #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?




Did Eric Clapton really think she looked wonderful...or was it after the 15th outfit she tried on and he just wanted to get to the party and get a drink?


BuffyNZ
241 posts

Master Geek


  #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


floydbloke

3522 posts

Uber Geek

ID Verified

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

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




Did Eric Clapton really think she looked wonderful...or was it after the 15th outfit she tried on and he just wanted to get to the party and get a drink?


Create new topic





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









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.