Mavershang
Mavershang

Reputation: 1278

c# Windows form application forms problem

I have a c# windows form app which contains several forms.

Generally, for example, in form1, I create a instance of form2 and then

form1.hide();
form2.show();

But sometimes I want the previous form to show and dispose current form. How can I call the previous form?

Thanks in advance.

Upvotes: 1

Views: 1418

Answers (6)

Kevin Stricker
Kevin Stricker

Reputation: 17388

You could do this in form1:

...
var form2 = new form2();
form2.Closing += (form2_Closing);
this.hide();
form2.show();
...


private void form2_Closing(object sender, System.EventArgs e)
{
    this.show();
}

Upvotes: 0

Tedd Hansen
Tedd Hansen

Reputation: 12326

This logic should be handled in Program.cs. The Main() method initializes Form1. You want to take control there instead of passing control to the form.

Example:

static class Program
{
    internal static Form1 MyForm1;
    internal static Form2 MyForm2;
    /// 
    /// The main entry point for the application.
    /// 
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //Application.Run(new Form1());

        // Initialize Form1
        MyForm1 = new Form1();
        MyForm1.FormClosing += new FormClosingEventHandler(MyForm1_FormClosing);

        // You may want to initialize Form2 on-demand instead of up front like here.
        MyForm2 = new Form1();
        MyForm2.FormClosing += new FormClosingEventHandler(MyForm2_FormClosing);

        // Show Form1 first
        MyForm1.Show();

        // Now we need to occupy the thread so it won't exit the app. This is normally the job of Application.Run.
        // An alternative to this is to have a third form you pass on control to.
        while (true)
        {
            Application.DoEvents();
            System.Threading.Thread.Sleep(10);
        }
    }

    static void MyForm1_FormClosing(object sender, FormClosingEventArgs e)
    {
        // Do something, for example show Form2
        MyForm2.Show();

        // EXAMPLE: We only want to hide it?
        e.Cancel = true;
        MyForm1.Visible = false;
    }
    static void MyForm2_FormClosing(object sender, FormClosingEventArgs e)
    {
        // Do something, for example show Form1
        MyForm1.Show();

        // EXAMPLE: We only want to hide it?
        e.Cancel = true;
        MyForm2.Visible = false;
    }
}

Since Program is static you can access MyForm1 and MyForm2 anywhere in that project by:

Program.MyForm1.Show();
Program.MyForm2.Hide();

If you plan to have many forms/complex logic I suggest moving this to a separate class. Also consider using a single form and rotate user controls inside it instead.

Upvotes: 1

Paul Sasik
Paul Sasik

Reputation: 81537

To answer your question, you need to maintain references in your views to each other. While this might work it's messy and error prone. It sounds like all your control logic is probably contained within your form class code and I would suggest moving away from that and separate your concerns.

Solving your form management issues becomes very simple if you create a controller class that, at a minimum, manages the creation and disposal of your forms in whatever way you see fit.

So your code sample would actually be launched from a controller class as something like:

public class FormsController
{
    private Form form1 = new Form();
    private Form form2 = new Form();

    public void SwitchForms()
    {
        form1.hide();
        form2.show();
    }
}

For further edification checkout the MVC architectural pattern for cleanly working with data, biz logic and UI.

Upvotes: 4

capdragon
capdragon

Reputation: 14899

Form2 myform = new Form2();

myform.show();
this.hide();

Upvotes: 0

KeithS
KeithS

Reputation: 71591

If you set the new form's Owner to a reference to the current form, you can reference that Owner from the new form. You could also subscribe to the new form's Closed() event from the old form, with code to dispose it (though the form can dispose itself by overriding OnClosed, if it doesn't happen there anyway).

Upvotes: 1

digitlworld
digitlworld

Reputation: 1046

You might consider extending Form to include some properties/fields that allow you to access other forms. the Form class can be inherited from just like most other .Net classes.

You may also consider doing some of that management in the Program.cs file that is part of you project, if neither form is really supposed to be a child of the other.

If you inherit a new class for your form1 from Form and add a method like closeSecondForm you can have it close and dispose the second form.

There are probably a bunch of different ways to solve the issue. These are just a few.

Upvotes: 1

Related Questions