michellechan57
asked on
VS 2005 - C# Event
Has anyone experienced this?
When a control (say a button) is moved (cut) to another container
(say a panel)(paste), the event originally assigned is gone. I have
to go to its property and manually re-assign again for it to work.
To overcome this problem , I have tried to assign the event programmatically
but the event will be fired more than once if it has already been assigned
using the designer.
Can I programatically check if the event has been assigned in the first place?
Thanks.
When a control (say a button) is moved (cut) to another container
(say a panel)(paste), the event originally assigned is gone. I have
to go to its property and manually re-assign again for it to work.
To overcome this problem , I have tried to assign the event programmatically
but the event will be fired more than once if it has already been assigned
using the designer.
Can I programatically check if the event has been assigned in the first place?
Thanks.
if (button1.Click ==null)
button1.Click += new EventHandler(this.button1_ Click);
button1.Click += new EventHandler(this.button1_
ASKER
Hi Desp,
Received following error message:
The event 'System.Windows.Forms.Cont rol.Click' can only appear on the left hand side of += or -=C:\WindowsApplication2\S ource.cs 32 25 MyApp
Received following error message:
The event 'System.Windows.Forms.Cont
Create a method as follows:
public void AddEventHandler(EventHandl er handler)
{
if (Event != null)
{
bool addHandler = true;
Delegate[] d = Event.GetInvocationList();
foreach (Delegate de in d)
{
if (handler.Target == de.Target)
{
addHandler = false;
break;
}
}
if (addHandler)
{
Event += handler;
}
}
else
{
Event += handler;
}
}
That way you will ensure that the handler will only get added if it hasnt been added already.
Hope that helps.
public void AddEventHandler(EventHandl
{
if (Event != null)
{
bool addHandler = true;
Delegate[] d = Event.GetInvocationList();
foreach (Delegate de in d)
{
if (handler.Target == de.Target)
{
addHandler = false;
break;
}
}
if (addHandler)
{
Event += handler;
}
}
else
{
Event += handler;
}
}
That way you will ensure that the handler will only get added if it hasnt been added already.
Hope that helps.
ASKER
Hi Seraph_78,
Where is Event declared and declared as what type?
If its button1.Click event then I probably will have the same error
message mentioned in the previous comment.
Thanks.
Where is Event declared and declared as what type?
If its button1.Click event then I probably will have the same error
message mentioned in the previous comment.
Thanks.
Event is just name for an event. So yes button1.Click.
The code ensures that you dont attach the same handler more than once.
If you are getting the error mentioned before are you sure you are attaching the handler to the event and not somehow having the event on the right hand side of the asignment.
The code ensures that you dont attach the same handler more than once.
If you are getting the error mentioned before are you sure you are attaching the handler to the event and not somehow having the event on the right hand side of the asignment.
ASKER
Hi,
The compiler error message occurred at the statement when I test if the event is null.
It occurred before any assignment of handler.
It says the "event" can only appear on the left of either += or -=.
Am I missing something here or I have misunderstood some important point?
Please help. Thanks.
The compiler error message occurred at the statement when I test if the event is null.
It occurred before any assignment of handler.
It says the "event" can only appear on the left of either += or -=.
Am I missing something here or I have misunderstood some important point?
Please help. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
button1.Click -= new EventHandler(this.button1_
button1.Click += new EventHandler(this.button1_
This doesn't fail also if currently there is no association.