I am writing a program that requires toggling between 2 forms. I thought I could use the Hide and Show methods but I need a way for the forms to know about each other so they can Hide themselves and Show the other form. Initially, form1 instantiates form2

Recommended Answers

All 5 Replies

You ca ndo it this way:

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            Form2 f2 = new Form2(f3);
            f2.Show();
        }
    }   

//form2:
 public partial class Form2 : Form
    {
        Form3 f3;
        public Form2(Form3 _f3)
        {           
            InitializeComponent();
            f3 = _f3;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f3 = new Form3();
            f3.Show();
        }
    }

I have a couple questions concerning this solution. First, clicking button1 on both form1 and form2 creates a new instance of form3. Seems like I would end up with many instances of form3 floating around.
Second, I only have 2 forms. Form1 loads on program initialization. After selecting item(s) from a list on form1, I click an ACCEPT button which instantiates and shows form2. From from2 I can act upon items selected in form1. I also have a BACK button on form2 which I need to take me back to form1 to select additional items which are added to the already existing list. Then I will have another button on form1 which will take me back to the already existing instance of form2.

simply hide the form2 (which will show from1). And when you need to show form2 again, just call form2.Show() method. thats it.

Mitja

Do you need to have both on their own form? You could have one form, and have the form show a panel that holds a custom control and just show and hide the different panels, that way it's all on the same form but "appears" to have two forms. It also prevents that annoying fade in and fade out effect for vista users. Just an idea...I use it a lot for "control panel" or "settings" forms.

I can post an example project if you would like to try that approach.

Do you need to have both on their own form? You could have one form, and have the form show a panel that holds a custom control and just show and hide the different panels, that way it's all on the same form but "appears" to have two forms. It also prevents that annoying fade in and fade out effect for vista users. Just an idea...I use it a lot for "control panel" or "settings" forms.

I can post an example project if you would like to try that approach.

I am always open to examples

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.