Solved

Click EVent Misunderstaninding

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now