Board index » cppbuilder » Sender??

Sender??

I've got a ListBox on my form, I've attached a PopupMenu to it that
looks like this,

Look - inside
     - underneath
etc, etc...

So when the user clicks on an item in the ListBox a menu appears, then
they can select a menu item, I have one function for the onClick event
of each sub menu item, then want to do the processing something like
this,

void TMainForm::Examining(TObject *Sender)
{
 Game.Process(MyListBox->ItemIndex, Sender->Caption);

Quote
}

So the Game engine will receive an item to look at and a string telling
it where to look.

But I can't access the Caption property of the MenuItem the user clicked
on. The only thing I could find in the help files was "thing" but that
just gives me the MainForms caption.
Anyone got any clues??

BTW I'm also subscribed to the
borland.public.cppbuilder.vcl
borland.public.cppbuilder.vcl.components
groups, but I'm not getting any posts that are being sent to them, are
they dead groups??

TIA

--
David Jenkins

 

Re:Sender??


Quote
> void TMainForm::Examining(TObject *Sender)
> {
>  Game.Process(MyListBox->ItemIndex, Sender->Caption);
> }

You'll have to get used to the following technique in BCB, as you'll be doing
it all the time.

When you get a TObject* Sender passed to your event handler, you need to
cast it to the item you wish to handle. So, if in this case it is a menu item,
you use dynamic_cast and use it:

  TMenuItem* item = dynamic_cast<TMenuItem*>(Sender);
  if ( !item )
   return;

  Game.Process(MyListBox->ItemIndex, item->Caption);

Using dynamic_cast and checking its result is the safest way. However, often
in an event handler you just *know* who the sender is, so you can just
use static_cast, which is probably a little faster:

 TMenuItem* item = static_cast<TMenuItem*>(Sender);

+===================================================+
| Jonathan Arnold (mailto:jdarn...@buddydog.org)    |
| http://www.buddydog.org                           |
+===================================================+

There comes a time when you have to face the fact that
Dad has forgotten how to do algebra. -- Pam Brown

Re:Sender??


Hi!
I don't think you need that. All you need is to use:
Game.Process(MyListBox->ItemIndex, ((TMenuItem*)Sender)->Caption);
If you know that the sender is a TMenuItem.

/Marcus

Quote
>   TMenuItem* item = dynamic_cast<TMenuItem*>(Sender);
>   if ( !item )
>    return;

>   Game.Process(MyListBox->ItemIndex, item->Caption);

Re:Sender??


In article <826chv$c...@forums.borland.com>, Marcus
<mest...@hem.passagen.se> writes

Quote
>Hi!
>I don't think you need that. All you need is to use:
>Game.Process(MyListBox->ItemIndex, ((TMenuItem*)Sender)->Caption);
>If you know that the sender is a TMenuItem.

Thanks guys (Narcus and Jonathan) I spotted a post in another group that
solved the problem, I'm using the
static_cast<TMenuItem*>(Sender)
I DO NOT like using it though, looks kind if messy, IYKWIM.
If the ((TMenuItem*)Sender)->Caption) works (I'll try it when/IF I'm
sober. ;) I'll adopt that, it looks a bit cleaner.

BTW I'm getting a bit concerned about this,
"
BTW I'm also subscribed to the
borland.public.cppbuilder.vcl
borland.public.cppbuilder.vcl.components
groups, but I'm not getting any posts that are being sent to them, are
they dead groups??
"
I'm not seeing any posts being made to those two groups, I only added
the components group a couple of days ago, but the root vcl group I've
had on for several months (5 or 6) Since switching ISP's (about 1.5
months ago) no posts to that NG, every other Borland (Inprise server) NG
seems okay.

WEere the using and component NG's added and the root vcl group died
since then??

--
David Jenkins

Re:Sender??


: David Jenkins <Da...@djenkins.greatxscape.net> wrote:

Quote
>static_cast<TMenuItem*>(Sender)
>I DO NOT like using it though, looks kind if messy, IYKWIM.
>If the ((TMenuItem*)Sender)->Caption) works (I'll try it when/IF I'm
>sober. ;) I'll adopt that, it looks a bit cleaner.

FWIW, the (TMenuItem*) is a disgusting, hard to track, impossible to debug
C-style cast.

The static_cast is a clean C++ cast, compile-time checked, easily
maintainable.

Quote
>BTW I'm also subscribed to the
>borland.public.cppbuilder.vcl
>borland.public.cppbuilder.vcl.components
>groups, but I'm not getting any posts that are being sent to them, are
>they dead groups??

Yes.

Quote
>I'm not seeing any posts being made to those two groups, I only added
>WEere the using and component NG's added and the root vcl group died
>since then??

Yes.

To learn more about this server and the group that it carries, have a look
at http://www.borland.com/newsgroups/

Thanks!

--
General information:
  * Post to the right group - http://www.borland.com/newsgroups/
    * Do not cross- or multipost
      * Research at http://www.mers.com/searchsite.html

Stefan Hoffmeister - http://www.econos.de/
(TeamB - http://www.teamb.com/)

Other Threads