0

I apologize if this has been addressed already, but I could not find a case that fit my exact situation. So here goes...

I have a MainForm that contains a toolStrip1 docked to the left that functions as a vertical navigation bar. I have a panel (pnlMain) filling up the remainder of the form. I want to use pnlMain to display different forms which are made up of win form classes. Right now, I can click on the labels/buttons on toolStrip1 to display different forms within pnlMain.

private void tsLblCustomers_Click(object sender, EventArgs e)
    {
        hidePanels();
        CustomerReport cr = new CustomerReport();
        cr.TopLevel = false;
        cr.AutoScroll = true;
        cr.BackColor = Color.White;
        pnlMain.Controls.Add(cr);
        cr.Show();

    }

What I want to do now is display additional forms within pnlMain by clicking on a button on another form rather than a label/button on toolStrip1. Some of my forms are as follows: CustomerReport, AddCustomer, EmployeeReport, AddEmployee. The Report forms are linked to my tool strip buttons. The Add forms are linked to buttons on the Reports forms. I tried several things including the following:

1) On CustomerReport, I tried creating an instance of MainForm, then I'll create an instance of AddCustomer, and then add that instance to the panel on MainForm. 2) I also tried creating a method in MainForm to create the instance of AddCustomer, and then call that method from the Add button on CustomerReport. Even though the code was the same as the toolstrip buttons on MainForm, it did not work.

I tried different variations of hiding forms, showing forms, clearing the panel, setting Visible to true or false, and I can't get it to work right. In some cases, I've managed to hide the CustomerReport, but AddCustomer will not come up. At some point I think I created a NEW instance of MainForm and my code wasn't impacting the original form that is already open. I'm just lost. Should I be using a different design? Originally I set up my application to just hide one form then show the other but I read that that is a 'terrible design'.

CaptainGenesisX
  • 328
  • 3
  • 10

2 Answers2

0

This sounds very similar to this thread here: Creating Form Inside the Form

You'd want to look into MDI.

Although it sounds like you're aiming for one cohesive interactive window. Otherwise, if you just want separate windows to popup, you can create properties within that other form and read them after returning a DialogResult. I'm not sure why this would be bad design without knowing more about the context of the program.

//Optionally do a hide(); here.
AddCustomer customer = new AddCustomer();
DialogResult result = customer.ShowDialog();
if(result == DialogResult.OK)
{
    var name = customer.Name;
    //More properties or whatever here.
}
//The properties would still be accessible here, too.
Jeffel
  • 31
  • 5
0

I ended up keeping the toolstrip nav bar on the left side of the primary window, and I created a panel in the main part of the window. All forms are displayed in the panel. Each time one of the label options in the nav bar is clicked on, the current form is cleared off the panel and the active form is displayed.

private void tsLblCustomers_Click(object sender, EventArgs e)
    {
        pnlMain.Controls.Clear();
        CustomerReport cr = new CustomerReport();
        cr.TopLevel = false;
        cr.AutoScroll = true;
        cr.BackColor = Color.White;
        pnlMain.Controls.Add(cr);
        cr.Show();
    }

    private void tsLblEmployees_Click(object sender, EventArgs e)
    {
        pnlMain.Controls.Clear();
        EmployeeReport emp = new EmployeeReport();
        emp.TopLevel = false;
        emp.AutoScroll = true;
        emp.BackColor = Color.White;
        pnlMain.Controls.Add(emp);
        emp.Show();

    }

    private void tsLblVendors_Click(object sender, EventArgs e)
    {
        pnlMain.Controls.Clear();
        VendorReport vend = new VendorReport();
        vend.TopLevel = false;
        vend.AutoScroll = true;
        vend.BackColor = Color.White;
        pnlMain.Controls.Add(vend);
        vend.Show();

    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        WelcomeForm welcome = new WelcomeForm();
        welcome.TopLevel = false;
        welcome.AutoScroll = true;
        welcome.BackColor = Color.White;
        pnlMain.Controls.Add(welcome);
        welcome.Show();
    }
CaptainGenesisX
  • 328
  • 3
  • 10