简体   繁体   中英

controling multiple forms show, hide or close with one form in c#

I want to be able to show, hide or close some other forms within one form. the idea is my program starts with one form and there are some buttons that will show other forms. That is if user clicks on a button for bringing on another form, this main form should minimize and whenever user close the other form this form will be back to normal or maximized state.

The code looks like this for my main form that controls other forms (Form1.cs):

namespace MultipleForms
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    /* 
     * I want to make a public reference to this Form1
     * So that when user closes Form2 That is F2 this form will be
     * set back to normal or maximized state
     */ 

    private void buttonStartForm2_Click(object sender, EventArgs e)
    {
        //Declare Form2
        Form2 F2 = new Form2();

        //Show Form2
        F2.Show();

        //Minimize Form1
        this.WindowState = FormWindowState.Minimized;

    }
}
}

And this is Form2.cs

    namespace MultipleForms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        //FormClosed event handler
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Closing Form2
            this.Close();

            //Now I need a code to make Form1 to recover from minimized
            // state to normal or maximized state. how do I should refer to it?
        }
    }
}

I think I have to do some stuff on form closing event, but how can I initilize the Form1.cs as something public? or I also should initilize Form1.cs in Form2.cs also? or maybe it should be done through Program.cs file:

namespace MultipleForms
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
}

PS: it is crtitical that Form2 should be closed, not hided

UPDATE

Using Hans solution, it is the best to use it this way:

    private bool isEnabledCmpFrm = false;

    public void buttonStartComparisonForm_Click(object sender, EventArgs e)
    {

        if (!isEnabledCmpFrm)
        {
            ComparisonForm CmpFrm = new ComparisonForm();
            CmpFrm.Show();
            CmpFrm.WindowState = FormWindowState.Normal;
            this.WindowState = FormWindowState.Minimized;

            isEnabledCmpFrm = true;

            CmpFrm.FormClosed += delegate
            {
                this.WindowState = FormWindowState.Normal;
                isEnabledCmpFrm = false;
            }; 
        }
    }

If I understand correctly, you can hook into the Form Closing event.

So in your main form you can do something like:

Form2 form2 = new Form2();
form2.Show();
form2.FormClosing += new FormClosingEventHandler(form2_FormClosing);


    void form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.Show();
    }

Don't have a form listen to its own events. The code needs to go in Form1. Like this:

        Form2 F2 = new Form2();
        F2.FormClosed += delegate { this.WindowState = FormWindowState.Normal; };
        F2.Show();
        this.WindowState = FormWindowState.Minimized;

您需要将对主窗体的引用传递给子窗体,并使用主窗体的show方法使其在子窗体关闭时再次可见。

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