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

3646 posts

Uber Geek
+1 received by user: 4554

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)




Sometimes I use big words I don't always fully understand in an effort to make myself sound more photosynthesis.


Create new topic
BuffyNZ
241 posts

Master Geek
+1 received by user: 27


  #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

3646 posts

Uber Geek
+1 received by user: 4554

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?




Sometimes I use big words I don't always fully understand in an effort to make myself sound more photosynthesis.


BuffyNZ
241 posts

Master Geek
+1 received by user: 27


  #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

3646 posts

Uber Geek
+1 received by user: 4554

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?




Sometimes I use big words I don't always fully understand in an effort to make myself sound more photosynthesis.


BuffyNZ
241 posts

Master Geek
+1 received by user: 27


  #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

3646 posts

Uber Geek
+1 received by user: 4554

ID Verified

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

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




Sometimes I use big words I don't always fully understand in an effort to make myself sound more photosynthesis.


Create new topic








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.