Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

Event related to a runtime created component.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marcelo - 30 May 2005 07:30 GMT
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.
Sebastian Dau - 30 May 2005 11:27 GMT
Hello,

> Hello!
> I am developping a Visual C++ .NET 2003 multiple forms application. My
[quoted text clipped - 3 lines]
> 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...
Ismail Pazarbasi - 30 May 2005 13:02 GMT
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
Marcelo - 30 May 2005 15:41 GMT
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.
Antti Keskinen - 30 May 2005 17:49 GMT
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

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

> Thanks very much! That worked!
> But now another problem came up:
[quoted text clipped - 4 lines]
> of that new specific item that suffered the click? These would solve my
> problem at all. Thanks once more.
Marcelo - 31 May 2005 14:47 GMT
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?
Sebastian Dau - 01 Jun 2005 08:18 GMT
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

> 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?
Marcelo - 01 Jun 2005 11:53 GMT
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!
Sebastian Dau - 01 Jun 2005 14:17 GMT
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

> 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!
Marcelo - 01 Jun 2005 15:52 GMT
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
Sebastian Dau - 02 Jun 2005 13:26 GMT
> Thank you for the suggestion.
> Could you help me in one more thing? I am working with 2 forms. For
[quoted text clipped - 15 lines]
>
> 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
Marcelo - 03 Jun 2005 08:18 GMT
Thank you once more! It worked perfectly.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.