C# Windows Forms code not working - Attach Event to button

前端 未结 2 449
别跟我提以往
别跟我提以往 2020-12-20 05:40

Could someone explain the reason why the code below does not work?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.         


        
相关标签:
2条回答
  • 2020-12-20 05:57

    You need to bind the button click to the event of button click! :)

    button1.Click += button1_Click;
    
    0 讨论(0)
  • 2020-12-20 06:08

    Make sure attached the event Click with your button. You can do it by going to designer, double click the button, it will create the event handler for you in the code. You can also attach the event handler in your Form Constructor like:

    public Form1()
    {
        InitializeComponent();
        button1.Click += button1_Click;
    }
    

    You can go to the designer, right click on Button1, click properties, Got to events and there you can attach the event handler:

    enter image description here

    0 讨论(0)
提交回复
热议问题