winform中无响应的关闭按钮

时间:2017-11-15 17:27:04

标签: c# winforms visual-studio-2015

我正在尝试在Visual Studio中创建一个Option对话框。我隐藏了默认的ControlBox并创建了一个关闭按钮。对话框工作,但关闭按钮不起作用。这是代码:

    public static class dialog
    {        
        static Form gotoBox = new Form();

        public static void showDialog()
        {
            Button closeButton = new Button() { Text = "Close" };                            

            gotoBox.Controls.Add(closeButton);
            gotoBox.ControlBox = false;
            gotoBox.ShowDialog();

            closeButton.Click += new System.EventHandler(gotoBox_close);               
        }

        static void gotoBox_close(object sender, EventArgs e)
        {
            gotoBox.Close();
        }

    }

单击按钮时,没有任何反应。那我做错了什么?

1 个答案:

答案 0 :(得分:1)

gotoBox.ShowDialog(); //This line shows the dialog

//The rest doesn't execute until ShowDialog returns
closeButton.Click += new System.EventHandler(gotoBox_close);     

您需要在显示对话框之前移动事件注册,否则在对话框关闭之前它不会有任何效果