Seven
Seven

Reputation: 441

How to replace multiple form in C#.Net?

I have a MainForm , form2 and form3 in my C#.Net application and two buttons in this MainForm.

The buttons' names are form2 and form3.

When I click the form2 button in MainForm, I would like form2 to replace the MainForm's UI. Note that I am not trying to navigate to another form.

Likewise: when I click the form3 button in MainForm, Form3's UI replaces the existing MainForm's UI.

If there is a codeproject for this functionality, please share a link.

Thank you for your time.

Upvotes: 0

Views: 1543

Answers (6)

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15148

You can use panels for that. Create 3 panels and hide 2 when needed. If I understood you correctly that is. Or a more elegant solution would be to use user controls.

Upvotes: 1

MisdartedPenguin
MisdartedPenguin

Reputation: 268

A not so nice way to do it but would require minimal coding is to use Panels.

Create 3 panels, put content into each panel. Make 2 of them invisible at a time.

Upvotes: 1

Sandy
Sandy

Reputation: 11687

Definetly not an expertise answer, but it may be of some help.

You can place all your main form, form2 and form3 contents in a Panel container (say panelmain, panel2, panel3). On button click event, just remove Panelmain from form1 and add panel2 or panel3 as required.

For button from2

private void btnForm2_Click(object sender, EventArgs e)
{
    Panelmain.Enabled = false; // optional
    this.Controls.Remove(Panelmain);
    Panel2.Enabled = true; // optional
    this.Controls.Add(Panel2);
}

For button from3

private void btnForm3_Click(object sender, EventArgs e)
{
    Panelmain.Enabled = false; // optional
    this.Controls.Remove(Panelmain);
    Panel3.Enabled = true; // optional
    this.Controls.Add(Panel3);
}

Upvotes: 1

PythEch
PythEch

Reputation: 952

Are you trying to make a Wizard?

If not or you have different designs for different forms:

Make a class via right-clicking the Project in Visual Studio > New Item, and paste this:

class form
{
    //Needs simple editing...
    public static Form1 form1 = new Form1();
    public static Form2 form2 = new Form2();
    public static Form3 form3 = new Form3();
    public static Form4 form4 = new Form4();
    public static Form5 form5 = new Form5();
}

And enter this code where you want to change forms:

//This will make show another form on the existing form's location
form.form2.Show();
form.form2.Location = this.Location;
this.Hide();

Upvotes: 1

GvS
GvS

Reputation: 52518

Instead of forms, place the UI in UserControls.

On your main form, add some logic to load a UserControl. From the buttons on the control, you can call a method that will place the correct control in the form.

For example:

 public class MyForm : Form {

      Control body;
      public void HandleUserAction(string action) {
          if (body != null) this.Controls.Remove(body);
          if (action == "goto1") {
             body = new MyUserControl();
          }
          // Handle other actions
          if (body == null) return;
          this.Controls.Add(body);
      }
 }

 // In your control

 public MyCtl : UserControl {
       public void Button1_Click(object sender, EventArgs e) {
           ((MyForm)FindForm()).HandleUserAction("goto1");
       }
 }

Upvotes: 1

ShdNx
ShdNx

Reputation: 3213

Well, this is a tricky question. As far as I know, there's no nice way to do it. There are several options though:

  • You can play around with the ApplicationContext class, and set a new main form for the application during the runtime. This allows you to close the main window when displaying the new one, without closing the entire application.
  • Hide the main window before showing the new window.
  • You can hide the controls belonging to the initial screen and unhide the ones belonging to the new window. (A little bit nicer way of doing this would be to design the windows separately, in different classes, then instead of displaying them, just copy the controls to the current form.)

Why do you want to do this?

Upvotes: 1

Related Questions