Solved

Click EVent Misunderstaninding

Posted on 2006-06-21
3
187 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
itextsharp with c# 3 33
C# XML Get Values 4 52
Read top line from CSV file 14 57
async questions 5 48
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 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