By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

Event related to a runtime created component.

P: n/a
Hello!
I am developping a Visual C++ .NET 2003 multiple forms application. My
problem is: When running my application, I click a button and a new
main menu item is created. Ok. Now I want to relate an event to the
click of this new item created. How do I create an event related to a
component that is created in runtime?
If somebody could help me anyway I thank very much.

Nov 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hello,

"Marcelo" <ma******@msn.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello!
I am developping a Visual C++ .NET 2003 multiple forms application. My
problem is: When running my application, I click a button and a new
main menu item is created. Ok. Now I want to relate an event to the
click of this new item created. How do I create an event related to a
component that is created in runtime?
If somebody could help me anyway I thank very much.

m_NewComponent->EventName += new ComponentsEventHandler ( this,
EventHandlingMethodName );

void EventHandlingMethodName ( object* source, YourComponentsEventArgs* ea )

{

//handle event

}

your couuld take a look into the FormDesigners InitalizeComponents Method to
see how events are registered there...
Nov 17 '05 #2

P: n/a
Try following:

class MyForm : public Windows::Forms::Form
{
// ...
void ButtonHandler(Object* psender, EventArgs *pevent)
{
MenuItem* pitem = new MenuItem;
pitem->Text = S"My Menu Item";
pitem->Click += new EventHandler(this,
&MyForm::NewMenuItemHandler); // you need // this?
m_pmainmenu->MenuItems->Item[0]->Add(pitem);
}

void NewMenuItemHandler(Object* psender, EventArgs* pevent)
{
// handle your event here
}
// ...
};

Hope that helps.
Ismail

Nov 17 '05 #3

P: n/a
Thanks very much! That worked!
But now another problem came up:
If I create in runtime, for exemple, 20 items. Afterwards, when I click
in one of these items, the event will occur, ok. It is the same for any
of these items. But I need also to obtain the 'Text' and 'Checked'
properties of the clicked new item. How could I obtain the properties
of that new specific item that suffered the click? These would solve my
problem at all. Thanks once more.

Nov 17 '05 #4

P: n/a
Hello !

When writing an event handler, the prototype has two parameters: the sender
of the event, and the arguments related to the event. The sender, in this
case, is the menu item on which the event occurred. This can be judged by
looking at

pitem->Click += new EventHandler(this, &MyForm::NewMenuItemHandler);

When the click handler fires, you can take a look at the properties of the
object that is passed as an argument. Try casting it into a MenuItem
pointer, and checking the Text property to see which menu item it was that
fired the event. Then do handling/check status querying accordingly..

-Antti Keskinen

------------------------------

"Marcelo" <ma******@msn.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Thanks very much! That worked!
But now another problem came up:
If I create in runtime, for exemple, 20 items. Afterwards, when I click
in one of these items, the event will occur, ok. It is the same for any
of these items. But I need also to obtain the 'Text' and 'Checked'
properties of the clicked new item. How could I obtain the properties
of that new specific item that suffered the click? These would solve my
problem at all. Thanks once more.

Nov 17 '05 #5

P: n/a
Thank you! But I don't find a way to casting the object passed as
argument into a main Menu pointer. Could you suggest me any way?

Nov 17 '05 #6

P: n/a
Hi Marcelo,
try:

void MyForm::m_mniMyMenuItem_Click(System::Object * sender,
System::EventArgs * e)
{
MenuItem* mni = dynamic_cast<MenuItem*>(sender);
if ( mni->Text->Equals ( "ExpectedText" ) )
{
//do your stuff here...
}
}

Greets, Sebastian

"Marcelo" <ma******@msn.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Thank you! But I don't find a way to casting the object passed as
argument into a main Menu pointer. Could you suggest me any way?

Nov 17 '05 #7

P: n/a
It worked! With this operator I can get all the properties of the
component that suffered the event. That's what I wanted. Thanks very
much, really!

Nov 17 '05 #8

P: n/a
You're welcome,

I'd like to suggest you to learn something about casting... it is an
essential practice in object oriented languages.

Take Care: using dynamic_cast<Type*>(Object) might return NULL (0) if the
cast failes....
that means if you cast some object to a type that it is not related too
it(inherited from).

If you are familiar with exception handling, you could use

try
{
TargetType* target = __try_cast<TargetType*>(source);
}
catch (InvalidCastException* ice)
{
//do some error handling
}

dynmic_cast returns NULL for invalid casts,
__try_cast throws an Exception.

Greets, Sebastian Dau

"Marcelo" <ma******@msn.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
It worked! With this operator I can get all the properties of the
component that suffered the event. That's what I wanted. Thanks very
much, really!

Nov 17 '05 #9

P: n/a
Thank you for the suggestion.
Could you help me in one more thing? I am working with 2 forms. For
exemple, here :

profileToMenu = new MenuItem(profileName);
profileToMenu->Click += new System::EventHandler(this,
&ConnectionPreferences::RunTimeItem_Click);

a menu item is created and related to the RunTimeItem_Click event od
the ConnectionPreferences class . Now, if I create a menuItem in
another Form, let us say "Form1" class, how would I relate the click of
this menu item (which is in Form1) to the same event RunTimeItem_Click
of the ConnectionPreferences Form?

menuInForm1 = new MenuItem(profileName);
menuInForm1 ->Click += new System::EventHandler(this */(???)*/,
&ConnectionPreferences::RunTimeItem_Click */(???)*/);

Greetings, Marcelo Schio

Nov 17 '05 #10

P: n/a

"Marcelo" <ma******@msn.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Thank you for the suggestion.
Could you help me in one more thing? I am working with 2 forms. For
exemple, here :

profileToMenu = new MenuItem(profileName);
profileToMenu->Click += new System::EventHandler(this,
&ConnectionPreferences::RunTimeItem_Click);

a menu item is created and related to the RunTimeItem_Click event od
the ConnectionPreferences class . Now, if I create a menuItem in
another Form, let us say "Form1" class, how would I relate the click of
this menu item (which is in Form1) to the same event RunTimeItem_Click
of the ConnectionPreferences Form?

menuInForm1 = new MenuItem(profileName);
menuInForm1 ->Click += new System::EventHandler(this */(???)*/,
&ConnectionPreferences::RunTimeItem_Click */(???)*/);

Greetings, Marcelo Schio


Quick and dirty:

public __gc class Form2 : public Windows::Forms
{
//...
public: void Form1_MenuItem1_OnClick ( Object* sender , EventArgs* ea )
{
//react on event
}
};

public class Form1 : Windows::Forms
{
//...
void InitForm2 ( )
{
Form2* form2 = new Form2 ();
menuInForm1 = new MenuItem(profileName);
menuInForm1 ->Click += new System::EventHandler(form2 ,
&Form2::Form1_MenuItem1_OnClick );

}
};
This is the idea of it very straight forward, hopefully helpful.

Greets, Sebastian Dau
Nov 17 '05 #11

P: n/a
Thank you once more! It worked perfectly.

Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.