Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建按钮点击事件c#_C#_Click - Fatal编程技术网

创建按钮点击事件c#

创建按钮点击事件c#,c#,click,C#,Click,我用电脑做了一个按钮 Button buttonOk = new Button(); 与其他代码一起,如何检测是否已单击创建的按钮? 并确保如果单击,表单将关闭?如果按钮位于表单类中: public MainWindow() { // This button needs to exist on your form. myButton.Click += myButton_Click; } void myButton_Click(o

我用电脑做了一个按钮

Button buttonOk = new Button();
与其他代码一起,如何检测是否已单击创建的按钮?
并确保如果单击,表单将关闭?

如果按钮位于表单类中:

    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();
    }
buttonOk.Click += new EventHandler(your_click_method);
(可能不完全是
EventHandler

在单击方法中:

this.Close();
如果需要显示消息框:

MessageBox.Show("test");

您需要一个事件处理程序,它将在单击按钮时触发。 这里有一条捷径-

  var button = new Button();
  button.Text = "my button";

  this.Controls.Add(button);

  button.Click += (sender, args) =>
                       {
                           MessageBox.Show("Some stuff");
                           Close();
                       };
但最好对按钮、事件等多了解一点

如果您使用VisualStudioUI创建一个按钮,并在设计模式下双击该按钮,这将创建事件并为您连接它。然后可以转到设计器代码(默认为Form1.designer.cs),在那里可以找到事件:

 this.button1.Click += new System.EventHandler(this.button1_Click);
您还将看到按钮的许多其他信息设置,例如位置等,这将帮助您按照自己的方式创建一个按钮,并提高您对创建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;
至于关闭窗体,它与关闭()一样简单;在事件处理程序中:

private void button1_Click(object sender, EventArgs e)
    {
       MessageBox.Show("some text");
       Close();
    }

创建
按钮
并将其添加到
表单。控件
列表可将其显示在表单上:

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
在此处创建按钮单击方法:

void buttonOk_Click(object sender, EventArgs e)
        {
            MessageBox.Show("clicked");
            this.Close(); //all your choice to close it or remove this line
        }

您需要附加事件处理程序
按钮ok.Click+=…
很抱歉对我的问题进行了一些更改,但我如何才能这样做,它将打开MessageBox MessageBox.Show(“一些文本”);很抱歉,我的问题有所改变,但我如何才能这样做,这样它将打开一个MessageBox不适合我,但thankyou@FroodleStirling您的表单上有按钮吗?不要在代码中创建新按钮,只需使用表单上的按钮即可。我更新了我的答案。也许你可以试试。首先把按钮放在表单上。它会显示“需要对象引用”@FroodleStirling,而不需要与您一起编码,我认为您只需要获得一本好书,或者在网上查找示例。这就是一切。我唯一能想到的另一个建议是确保您的按钮在表单和代码中的名称相同(myButton或其他名称)。@Bob Horn:EventHandler在哪里?它是如何工作的?