Event related to a runtime created component.

  • Thread starter Thread starter Marcelo
  • Start date Start date
M

Marcelo

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.
 
Hello,



Marcelo said:
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...
 
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
 
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.
 
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
 
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?
 
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
 
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!
 
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
 
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
 
Marcelo said:
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
 
Back
Top