C# 使用“动态”按钮关闭动态创建的表单

C# 使用“动态”按钮关闭动态创建的表单,c#,dynamic,C#,Dynamic,我试图用一个动态按钮关闭一个动态创建的表单(这是我最简单的工作,我也在添加其他按钮来完成其他工作,但我觉得这是一个很好的开始) 现在我可以为该按钮创建表单、按钮和单击事件,但是我不知道在单击事件函数中添加什么来关闭该按钮的主机。我猜我可以通过单击功能访问按钮?或者将表单控件作为参数传递到函数中?感谢您的帮助 //Create form Snapshot snapshot = new Snapshot(); snapshot.StartPositio

我试图用一个动态按钮关闭一个动态创建的表单(这是我最简单的工作,我也在添加其他按钮来完成其他工作,但我觉得这是一个很好的开始)
现在我可以为该按钮创建表单、按钮和单击事件,但是我不知道在单击事件函数中添加什么来关闭该按钮的主机。我猜我可以通过单击功能访问按钮?或者将表单控件作为参数传递到函数中?感谢您的帮助

        //Create form
        Snapshot snapshot = new Snapshot();
        snapshot.StartPosition = FormStartPosition.CenterParent;

        //Create save button
        Button saveButton = new Button();
        saveButton.Text = "Save Screenshot";
        saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
        saveButton.Click += new EventHandler(saveButton_buttonClick);

        //Create exit button
        Button exitButton = new Button();
        exitButton.Text = "Exit";
        exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);

        //Add all the controls and open the form
        snapshot.Controls.Add(saveButton);
        snapshot.Controls.Add(exitButton);
        snapshot.ShowDialog();
我的点击事件功能看起来非常正常:

    void saveButton_buttonClick(object sender, EventArgs e)
    {


    }

不幸的是,我不知道该添加什么才能使函数正常工作!提前感谢任何人能给我的帮助!我觉得这应该是一个需要解决的直截了当的问题,但我还没有弄明白…

虽然使用命名函数确实可以做到这一点,但在类似这样的情况下,只使用匿名函数通常更简单:

Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;

//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += (_,args)=>
{
    SaveSnapshot();
};

//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
exitButton.Click += (_,args)=>
{
    snapshot.Close();
};

//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();

虽然使用命名函数当然可以做到这一点,但在以下情况下,仅使用匿名函数通常更简单:

Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;

//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += (_,args)=>
{
    SaveSnapshot();
};

//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
exitButton.Click += (_,args)=>
{
    snapshot.Close();
};

//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();

一种简单的方法是使用lambda方法:

Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Click += (s, e) => { shapshot.Close(); };

一种简单的方法是使用lambda方法:

Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Click += (s, e) => { shapshot.Close(); };

精彩的这实际上更符合我的目的。感谢您的帮助!精彩的这实际上更符合我的目的。感谢您的帮助!