简体   繁体   中英

C#, How to Consistantly bring a form to the front?

I have a MainForm Class Instance, I bring up another form like so;

InputForm MyInput= new InputForm("Enter a Number");
MyInput.ShowDialog();

I close MyInput form from within itself like this;

    private void Button_Click(object sender, EventArgs e)
    {
        //Do things here
        this.Hide();

    }

Flow resumes in the MainForm and using either

this.Show();

or

this.Activate();

neither will consistantly bring the MainForm to the front. How can I do this?

What you need to do is show your InputForm like this. This form of ShowDialog assigns the owner to your dialogbox.

DialogResult dr = MyInput.ShowDialog(this);
//test for result here

MyInput.Close();

this.Hide() appears to be hiding the main form, not the input. Because ShowDialog is a blocking method, the InputForm will need to be closed by user action, code internal to the InputForm, or by another thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM