?
Solved

VS 2005 - C# Event

Posted on 2006-06-11
8
Medium Priority
?
835 Views
Last Modified: 2008-03-05
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.
0
Comment
Question by:michellechan57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16883420
You can remove existing event handler before adding new one:

button1.Click -= new EventHandler(this.button1_Click);
button1.Click += new EventHandler(this.button1_Click);

This doesn't fail also if currently there is no association.
0
 
LVL 19

Expert Comment

by:Fahad Mukhtar
ID: 16883641
if (button1.Click ==null)
button1.Click += new EventHandler(this.button1_Click);
0
 

Author Comment

by:michellechan57
ID: 16884178
Hi Desp,
Received following error message:

The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=C:\WindowsApplication2\Source.cs      32      25      MyApp

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Seraph_78
ID: 16890706
Create a method as follows:

        public void AddEventHandler(EventHandler 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.
0
 

Author Comment

by:michellechan57
ID: 16891140
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.
0
 

Expert Comment

by:Seraph_78
ID: 16891152
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.
0
 

Author Comment

by:michellechan57
ID: 16891574
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.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16892014
Any chance that you will try my solution, wich both compiles and works?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question