What if when a user press a button, the application will load another form but the previous form is automatically hidden in the screen, afterwhich when the current form is closed the previous form is automatically activated? How can I do this?

I make use of ShowDialog() but the previous form is still visible. Can anybody help me?

Recommended Answers

All 4 Replies

private void button1_Click(object sender, System.EventArgs e)
{
	Form2 jx = new Form2() ;
	jx.Show();
	this.Hide(); 
}

do the same think in form2 to form1

Make sure you understand the difference between "modal" and "non-modal" forms.
When you use ShowDialog() this causes the form to be loaded as a modal form. A modal form retains focus (for the application) until it is closed. The form that calls this modal form is suspended until the modal form is closed.

The use of the Show() method will cause the form to be loaded as a non-modal form, and not suspend the caller form. This allows both forms to be active, and accept user input on either form.

Typically you will use the ShowDialog method when you want to display some information or gather user input that is required before the user can continue some main task. The sub-form is closed, and the main application continues.

You can use the Hide() method on non-modal forms (The main form is defaulted to non-modal). This will simply hide the form from the desktop. Calling Show() again will bring it back.

All that being said, you should be able to show and hide forms at will. You should consider the implications of using multiple non-modal forms. In many cases, information is shared between forms, or atleast between the main form and the sub-form. If this is the case, you can create the sub-form variable outside of a method, and allow the main form and the other sub-forms to use this variable to pass information, call its methods, etc. If you do this, you will want to check to make sure it is not null before making such calls.
You should also add an event handler to the sub-form's FormClosed event so that it will set its variable in the main form back to null when it closes. This cleans up memory, and allows you to check to see if it is instantiated before trying to use its methods, properties, etc.

Example:
   public partial class Form1 : Form
    {
        private Form2 myForm = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            myForm = new Form2();
            myForm.FormClosed += new FormClosedEventHandler(myForm_FormClosed);
            myForm.Show();
            //Hide();
        }

        void myForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            myForm.Dispose();
            myForm = null;
            button1.Enabled = true;
            //Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Loaded");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (myForm != null)
                myForm.setLabel("Hello World");
        }

    }

In this example, I have a main form, with two buttons. The first button launches the second form. The second button calls one of the form2's methods. But only if it is loaded.
I have commented out the Hide() and Show() methods so you can try out that feature as well.

Thanks. It perfectly worked for me...

Hi My name is Bilal and I'am doing research work on WPF in C#......Well as far as I know there are 2 ways to work between different forms....well I want is that if the user clicks a button....A new form should appear on the same form.....We can do this by creating user Controls.The second method is that I open the new form by using the .show() command..Well I wantto know that is there any third appropriate or a better solution to do my work more efficiently..I couldn't find the way to enable the MDI property of the form and create parent and CHILD..If you could help me with this I would be very greatful to you...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.