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. 11 1175
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...
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
------------------------------
"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.
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
"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?
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
"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!
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" <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
Thank you once more! It worked perfectly. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Verde |
last post by:
I'm using a 3rd party component in an ASP.NET 1.1 Web application. The
component has a .Click event that can be fired from the client, with an
associated event procedure in the code-behind module....
|
by: Verde |
last post by:
This is admittedly an apparently odd request... but please indulge me if you
don't mind:
Suppose I have two <asp:Button.../> on a page (Button1 and Button2). User
clicks Button1 and triggers a...
|
by: Amit D.Shinde |
last post by:
I am adding a new picturebox control at runtime on the form
How can i create click event handler for this control
Amit Shinde
|
by: Charles Law |
last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my
question about removing event handlers.
> http://www.vbinfozine.com/t_bindevt.shtml
Following on from that post, the...
|
by: MLM450 |
last post by:
I have created a .NET component in C# that has 2 interfaces. One is an
incoming interface with methods being exposed to COM-aware clients. The
other is an outgoing event interface that the...
| |
by: Rob Peterson |
last post by:
Hi there,
Does anyone know how to actually "abort" the event (component change)
described in the following link. Apparently it's doable as mentined in the
"Remarks" section but how....
|
by: Marc |
last post by:
Hi,
How can i increase the mousehover event timer?
AddHandler NewBtn.MouseHover, AddressOf Shownotes
Private Sub Shownotes(ByVal sender As System.Object, ByVal e As
System.EventArgs)...
|
by: dmeglio |
last post by:
Hello,
I'm aware that when an EventHandler is created, it creates a reference
to the object, therefore preventing GCing.
Therefore, I've been implementing IDisposable in my controls to...
|
by: ZBINContact |
last post by:
I am creating a self-checking set of usercontrols. They tend to call
their self-checking functionally in the "Load" event. I have run into
a problem with my TextBox usercontrol, however, as the...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |