C# Windows 窗体代码不起作用 - 将事件附加到按钮 [英] C# Windows Forms code not working - Attach Event to button

查看:38
本文介绍了C# Windows 窗体代码不起作用 - 将事件附加到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么下面的代码不起作用吗?

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Speaker
{    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("bravo you did it");
    }
}
}

我设计的与此代码对应的窗口是一个带有单个按钮的窗口.我打算做一个相当扩展的程序,但遇到问题,我决定从一个小样本开始,看看有什么问题,我看这个简单的代码都不起作用.有什么建议?当我按下按钮 1 时,什么也没有发生.

The window I have designed that corresponds to this code is a single window with a single button. I have the intention to do quite an extented program, but encountering problems, I decided to start with a small sample to see what's wrong, and I see neither this simple code works. Any suggestions? When I press the button1, nothing happens at all.

推荐答案

确保将事件 Click 附加到您的按钮上.您可以通过转到设计器,双击按钮来完成,它将在代码中为您创建事件处理程序.您还可以在表单构造函数中附加事件处理程序,例如:

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;
}

您可以转到设计器,右键单击 Button1,单击属性,转到事件,然后您可以在此处附加事件处理程序:

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

这篇关于C# Windows 窗体代码不起作用 - 将事件附加到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆