Member Avatar for jacg4

Here is the issue:
I have a multi-form application developed on C# with two forms, after I press a button I hide the first form and show the second, but how can I show the first form again if I close the second one pressing the red close button on the upper right of my window?
The FormClosing event doesn´t get this action...
Plz help me and Thanx for any answer

What you might want to consider is the following:

Disable the X close option for form 2 and instead put a button within the form that will perform the tasks of re-showing the first form and closing the 2nd.

Alternately, the FormClosing event SHOULD catch the close of the form and allow you to use the event handler to re-display the prior form with the possible exception being if the form you are closing is a modal dialog box as this is not disposed when the close is clicked but rather rendered hidden.

While I'm sure you've read it already this page has some resource information on the use of the formclosing event that may or may not help.

create a new event handler in Form2.designer
ex:

this.form2_unload += new System.Windows.Forms.EventHandler(this.Form2_unload);

then:

private MainForm gg;

    public Form2(Project1.Form1 p)
    {
        InitializeComponent();
        this.Owner = p;
        gg = p;

     }
    private void Form2_unload(object sender, EventArgs e)
    {
       gg.form1.show();  //or form1.visible = true;
       this.close();
    }

use

form.show();

on button click event first write

form1.show

and then close the current form that is

form2.close()

ypu habe to insure that first you have to open form then closethe current form tyr it

Member Avatar for jacg4

Thanx everyone I'll give it a try

Don't forget to mark the thread solved if your problem is resolved by any of the above suggestions or on your own. That way people don't think the issue is ongoing :)

Member Avatar for jacg4

Ok I made it!!! Thanx for the help guys
I ended up using the FormClosed event.
Modified the constructor on the second form to recive the first form and then showed the first within the FormClosed event.

Just as an aside...if you have created and shown the second form within the first, you can add an event handler within the first form to catch the closing of the second form. (is that as confusing as it seems to me? lol)

Like this:

public partial class frmLogin : Form
    {

        //create instance of form to show when user logs in
        private frmMainMenu mainMenu = new frmMainMenu();

        public frmLogin()
        {
            InitializeComponent();

            //bind handler to form closed event so login form can react to main menu being closed
            mainMenu.FormClosed += new FormClosedEventHandler(mainMenu_FormClosed);
                 
        }
        
        //when main menu is closed, unhide login form
        void mainMenu_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Visible = true;
        }

        //button to test login process
        private void button1_Click(object sender, EventArgs e)
        {
            //hide login form
            this.Visible = false;

            //reset main form - clear any changes made previously and reinstantiate if disposed
            mainMenu = new frmMainMenu();

            //show main menu
            mainMenu.Show();
        }

    }
Member Avatar for jacg4

(Yeah It is kind of confusing lol.)
But I used the form closing event on the second form and passed the first form instance to the second form constructor hehe,
BTW tnx cause I didn't realize i had to reset the second form when i close it.

yes, if you close it then try to call .show without reinstantiating it you will get an ObjectDisposedException

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.