简体   繁体   中英

Event handlers in Visual Studio

When I try to create an event handler eg. button click in c#, if I write the code manually in code behind class, the event handler won't be called; Whereas if I double click the button on the form and the VS auto generates the event, it will work perfectly. What is the reason behind this behavior?

There is a Form.Designer.cs file that hooks up the events for each individual form (each form has it's own designer file).

Open that and you'll find lines like:

button1.Click += button1_Click;

..etc.

As below:

设计师档案

点击接线

Your file isnt able to know that the event handler is to be linked to the function you have created.

If you want to link manually, click on the button once, and in the top section of the properties window, you'll see a lightning symbol(events). Click on that, and scroll down to the click event, and over there, add your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM