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.


1680 posts

Uber Geek
+1 received by user: 798


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)




Most of the trouble in the world is caused by people wanting to be important. (T.S. Eliot)


Create new topic
Will not stab you
230 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



1680 posts

Uber Geek
+1 received by user: 798


  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?




Most of the trouble in the world is caused by people wanting to be important. (T.S. Eliot)


Will not stab you
230 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



1680 posts

Uber Geek
+1 received by user: 798


  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?




Most of the trouble in the world is caused by people wanting to be important. (T.S. Eliot)


Will not stab you
230 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



1680 posts

Uber Geek
+1 received by user: 798


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

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




Most of the trouble in the world is caused by people wanting to be important. (T.S. Eliot)


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:



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.