创建按钮单击事件 c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15323733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Created Button Click Event c#
提问by Froodle
I have made a button using
我已经使用了一个按钮
Button buttonOk = new Button();
along with other code, how can I detect if the created button has been clicked? And make it that if clicked the Form will close?
连同其他代码,如何检测创建的按钮是否已被单击?并使它如果单击表单将关闭?
采纳答案by Bob Horn
public MainWindow()
{
// This button needs to exist on your form.
myButton.Click += myButton_Click;
}
void myButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Message here");
this.Close();
}
回答by ppetrov
if your button is inside your form class:
如果您的按钮在您的表单类中:
buttonOk.Click += new EventHandler(your_click_method);
(might not be exactly EventHandler
)
(可能不完全是EventHandler
)
and in your click method:
并在您的点击方法中:
this.Close();
If you need to show a message box:
如果您需要显示消息框:
MessageBox.Show("test");
回答by NDJ
You need an event handler which will fire when the button is clicked. Here is a quick way -
您需要一个在单击按钮时触发的事件处理程序。这是一个快速的方法 -
var button = new Button();
button.Text = "my button";
this.Controls.Add(button);
button.Click += (sender, args) =>
{
MessageBox.Show("Some stuff");
Close();
};
But it would be better to understand a bit more about buttons, events, etc.
但是最好多了解一下按钮、事件等。
If you use the visual studio UI to create a button and double click the button in design mode, this will create your event and hook it up for you. You can then go to the designer code (the default will be Form1.Designer.cs) where you will find the event:
如果您使用 Visual Studio UI 创建一个按钮并在设计模式下双击该按钮,这将创建您的事件并为您连接。然后,您可以转到设计器代码(默认为 Form1.Designer.cs),您将在其中找到事件:
this.button1.Click += new System.EventHandler(this.button1_Click);
You will also see a LOT of other information setup for the button, such as location, etc. - which will help you create one the way you want and will improve your understanding of creating UI elements. E.g. a default button gives this on my 2012 machine:
您还将看到按钮的许多其他信息设置,例如位置等 - 这将帮助您以您想要的方式创建一个,并提高您对创建 UI 元素的理解。例如,我 2012 年的机器上有一个默认按钮:
this.button1.Location = new System.Drawing.Point(128, 214);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
As for closing the Form, it is as easy as putting Close(); within your event handler:
至于关闭Form,就像放Close()一样简单;在您的事件处理程序中:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("some text");
Close();
}
回答by Shaharyar
Create the Button
and add it to Form.Controls
list to display it on your form:
创建Button
并将其添加到Form.Controls
列表中以在表单上显示它:
Button buttonOk = new Button();
buttonOk.Location = new Point(295, 45); //or what ever position you want it to give
buttonOk.Text = "OK"; //or what ever you want to write over it
buttonOk.Click += new EventHandler(buttonOk_Click);
this.Controls.Add(buttonOk); //here you add it to the Form's Controls list
Create the button click method here:
在此处创建按钮单击方法:
void buttonOk_Click(object sender, EventArgs e)
{
MessageBox.Show("clicked");
this.Close(); //all your choice to close it or remove this line
}