Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide main form, start new form, switch between the two without closing second form

I've got two forms, with subForm being called/created by a buttonClick in Form1. Right now I can initiate subForm, hide Form1, and then unhide Form1 when subForm is closed. What I'd like to be able to do is:

  1. If user clicks changeform button, check to see if subForm is active but hidden
  2. If no, then initiate subForm, else hide Form1, unhide subForm and pass control to it
  3. If user clicks subForm's changeform button, hide subForm, unhide Form1 and pass control to it
  4. If user clicks the "X" in the upper right corner of the form, then close the application, regardless of which form is active. (Right now, selecting the "X" closes the subForm and opens/unhides Form1.)

I can find solutions that do part of the requirements (and maybe all, I'm just too noob to know). To repeat from my previous question here, the code I have so far is:

Form1

    private void countClick(object sender, EventArgs e)
    {
        this.Hide(); 
        subForm myNewForm = new subForm();
        myNewForm.ShowDialog();
        this.Show();
        countSelect.Checked = false;
    }

and subForm

    private void totalClick(object sender, EventArgs e)
    {
        this.Close();
    }

This works, but it's not really elegant.

like image 663
Peter Ladd Avatar asked Dec 02 '25 09:12

Peter Ladd


1 Answers

I think the best way to do this is to roll your own ApplicationContext. This allows you full control over the application lifetime without having it being tied to a specific Window. See http://msdn.microsoft.com/en-us/library/ms157901.aspx for more information.

Here's an example:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyApplicationContext());
    }
}

public class MyApplicationContext : ApplicationContext
{
    public MyApplicationContext()
    {
        ShowForm1();
    }

    public void ShowForm1()
    {
        if (_form2 != null)
            _form2.Hide();
        if (_form1 == null)
        {
            _form1 = new Form1(this);
            _form1.FormClosed += OnFormClosed;
        }
        _form1.Show();
        MainForm = _form1;
    }

    public void ShowForm2()
    {
        if (_form1 != null)
            _form1.Hide();
        if (_form2 == null)
        {
            _form2 = new Form2(this);
            _form2.FormClosed += OnFormClosed;
        }
        _form2.Show();
        MainForm = _form2;
    }

    private void OnFormClosed(object sender, FormClosedEventArgs e)
    {
        if (_form1 != null)
        {
            _form1.Dispose();
            _form1 = null;
        }
        if (_form2 != null)
        {
            _form2.Dispose();
            _form2 = null;
        }
        ExitThread();
    }

    private Form1 _form1;
    private Form2 _form2;
}

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

    public Form1(MyApplicationContext context)
        : this()
    {
        _context = context;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_context != null)
            _context.ShowForm2();
    }

    private readonly MyApplicationContext _context;
}

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

    public Form2(MyApplicationContext context)
        : this()
    {
        _context = context;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_context != null)
            _context.ShowForm1();
    }

    private readonly MyApplicationContext _context;
}
like image 177
Michael Gunter Avatar answered Dec 04 '25 00:12

Michael Gunter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!