Solved

Click EVent Misunderstaninding

Posted on 2006-06-21
3
189 Views
Last Modified: 2010-04-16
Hi, please can somebody explain to me how events works with C# Windows Programming. I mean take this for example.  In the form I add a button, I then double click this button and it takes me into the code, where it generates an event.

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

Now, I thought ok, I will create one manually without double clicking it. So I add another button underneathe it called button2 and then I go into the code and manually type this

        private void button2_Click(object sender, EventArgs e)
        {
           
        }

When I double click the button and it generates the code then it will fire the Click event. But in the case of button2, when I manually write the code it does not fire.

I am obviously missing the method of attaching the event to the button.

thanks in advance

Andrew
0
Comment
Question by:REA_ANDREW
[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
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 16949576
Right-click on button1_Click and select "Find all references". You will see the following line generated by Windows Form Designer when you double-click button:

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

Designer-generated code is placed in separate file (2005 version) or in the same file (precious versions). This line subscribes to Click event. To do the same manually add the following code to form constructor or Load handler:

this.button2.Click += new System.EventHandler(this.button2_Click);
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16949597
GOTCHA

So all events I want to declare for any object, I put in the Form.Designer.cs file , under the intialize component.

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16949767
No, Form.Designer.cs file is file managed by Form Designer, don't make any changes in it. You have Form.cs file to write your own code.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

631 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