与运行时创建的组件相关的事件。 [英] Event related to a runtime created component.
问题描述
您好!
我正在开发Visual C ++ .NET 2003多表单应用程序。我的
问题是:运行我的应用程序时,我点击一个按钮,然后创建一个新的
主菜单项。好。现在我想将一个事件与创建的这个新项目的
点击相关联。如何创建与在运行时创建的
组件相关的事件?
如果有人可以帮助我,我非常感谢。
>
推荐答案
您好,
" Marcelo" <毫安****** @ msn.com>在消息中写道
news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
Hello,
"Marcelo" <ma******@msn.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
您好!
我正在开发Visual C ++ .NET 2003多表单应用程序。我的问题是:运行我的应用程序时,我单击一个按钮,然后创建一个新的主菜单项。好。现在我想将一个事件与创建的这个新项目点击相关联。如何创建与在运行时创建的组件相关的事件?
如果有人可以帮助我,我非常感谢。
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)
>
{
//处理事件
}
你的couuld take看一下FormDesigners InitalizeComponents方法,然后看看
如何在那里注册事件......
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...
试试以下内容:
>
class MyForm:public Windows :: Forms :: Form
{
// ...
void ButtonHandler(Object * psender,EventArgs * pevent)
{
MenuItem * pitem = new MenuItem;
pitem-> Text = S" My Menu Item" ;;
pitem->点击+ = new EventHandler(这,
& MyForm :: NewMenuItemHandler); //你需要//这个吗?
m_pmainmenu-> MenuItems-> Item [0] - > Add(pitem);
}
void NewMenuItemHandler(Object * psender,EventArgs * pevent)
{
//在这里处理您的活动
}
// ...
};
希望有所帮助。
Ismail
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
非常感谢!这很有效!
但是现在出现了另一个问题:
如果我在运行时创建,例如20项。之后,当我在其中一个项目中点击
时,事件将会发生,好的。这些物品的任何
都是一样的。但是我还需要获得点击的新项目的''Text''和''Checked''
属性。我如何获得遭受点击的新特定项目的属性
?这些将解决我的
问题。再次感谢。
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.
这篇关于与运行时创建的组件相关的事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!