Let's think about a small program that has a form and two panels. There is a menu strip at the top and under the "File" menu there are 2 options: "New Form" and "Admin" (and "Quit"). There are 60 fields in different groupboxes in panel1 (a detailed registration form). A new form, panel1, is automatically shown when program is run. From the file menu, when user clicks "admin" option, panel1 should disappear and panel2 should become visible. Since both panels have common fields and functions to control/restrict input, I think I should use the same form. My current algoritm is as follows:

* When "New Form" is clicked:
- if panel1 is current one: clear every field in panel1
- if panel2 is current one: clear every field in panel2, hide panel2, show panel1

* When "Admin" is clicked:
- if panel1 is current one: clear every field in panel1, hide panel1, show panel2
- if panel2 is current one: clear every field in panel2

Is this mechanism correct or what is the best way to switch between different forms/panels in C#?

Hello, I have a project with some similar aspects. My project is for writing INVOICES and QUOTES. I use a single panel with text boxes and an int "MODE" which I change from INVC to QUOT and back. I actually keep the same text boxes and change their content. I build a string for each line and save it in memory when I switch away from one mode and then restore the contents back into the boxes when I return to that mode.

As an alternative consider this:
Place your panel 2 in front of your panel 1. If panel 2 is NOT visible, then panel 1 will be visible. When you make panel 2 visible, it "covers" panel 1 which effectively makes it invisible. When panel 1 is not visible, it makes no difference whether to contents of the text boxes are cleared or not.

Be careful when hiding controls behind others.
The TAB button will still move the focus to any "visible" controls!
This can be confusing for users.

I recommend either setting panel1.Visible = !panel2.Visible; or panel1.Enable = !panel2.Visible; when panel2 is made visible or hidden.
Either of these will prevent TAB from moving to the controls in panel1 when panel 2 is visible.

Good advice!!

thank you, I will enhance it as you point out and thank you all for your support. I think my mechanism is fine enough and not so weird for a simple program.

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.