c#中在代码中定义了一个Button控件,如何添加该控件的单击事件。事件实现当打击该控件时,该控件隐藏起来
c#中在代码中定义了一个Button控件,如何添加该控件的单击事件。事件实现当打击该控件时,该控件隐藏起来。谢谢啦
程序代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
var but = new Button { Text = "ButtonText"/*其它属性如位置大小等*/ };
Controls.Add(but);
but.Click += but_Click;
}
static void but_Click(object sender, EventArgs e)
{
((Button) sender).Visible = false;
}
}
}








