Bh Bb
Bh Bb

Reputation: 33

non-responsive close button in winform

I'm trying to make an Option dialog in Visual Studio. I hid the default ControlBox and made a close button. The dialog work, but the close button isn't. Here's the code:

    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();
        }

    }

When I click the button, nothing happen. So what did I do wrong?

Upvotes: 1

Views: 158

Answers (1)

Steve
Steve

Reputation: 11963

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

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

You need to move the event registration before the show dialog or else it will not have any effect until dialog is closed

Upvotes: 1

Related Questions