Solved

Click EVent Misunderstaninding

Posted on 2006-06-21
3
182 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

15 Experts available now in Live!

Get 1:1 Help Now