Click here to Skip to main content
16,018,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1. I have 3 forms ,form 1,form 2,form 3.
2. I open form 2 and form 3 by clicking different buttons in form 1;
3. I want to close form 2(if opened) when i click a button to open form 3;
and vice versa
how this possible?? pls help
Posted

1 solution

Why all that? OK, just use the method System.Windows.Forms.Form.Close.
That's not all. The problem is a little bit less trivial.

The show the same form again (just use Form.Show, not matter if the form was closed before or not), you need to prevent form disposal. This is done via handling the event Form.FormClosing, .

C#
//call it from the form constructor:
this.FormClosing += (sender, evenArgs) => {
    if (evenArgs.CloseReason != CloseReason.ApplicationExitCall) {
        evenArgs.Cancel = true;
        this.Hide();
    } //if
};


Alternatively, override the method System.Windows.Forms.Form.OnFormClosing to get the same effect. The check of CloseReason is needed to allow regular action on closing of the application.

Important: If this trick is applied to the main form as well, it can create the following problem: how to close the application? Well, you will need to add a control or a menu item to do that; it should call Application.Exit explicitly.

Overall, the style of UI you're trying to implement is pretty bad: not only it's harder to implement; it is also confusing the users. Consider the following: you use just one main window, but it changes its appearance because it is a parent of 3 panels filling the form client area; you show only one panel at a time. Alternatively, create just a tabbed UI based on System.Windows.Forms.TabControl — this is the easiest and a very natural way.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900