Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
0 down vote

favorite





I am using a static form object to call the next form to show. I have a controlling form that contains the static form object that calls a menu. When I click on a menu option, the form object is updated with the name of the desired form. The controller then calls the ShowDialog(this) method.

This works up to a point.

The flow of the program goes like this,

The controller calls a login form which creates a user object.

If the login is successful, the Main Menu form is called.

The user clicks on a button to select the desired option which assigns the appropriate sub menu to the static form object.

When the user clicks on an option on the sub menu the static object is updated.

I use the new keyword to assign the values to the static object, and issue a call to the Close() method (I've tried calling the Dispose() method as well).

Control is supposed to be passed back to the controlling form, however the main menu reassigns the value the user selected to the static object, even though it has been closed.

I get a Cannot access disposed object error when I call the ShowDialog(this) method in the controlling form.

Any suggestions are greatly appreciated.
Posted
Comments
Ravi Bhavnani 12-Sep-14 15:52pm    
Please post representative parts of your code.

/ravi

1 solution

Here is the code

C#
    public static Form nextForm;

    private Company company = new Company();

    public LoginDAL loginDAL = new LoginDAL();

    public CompanyDAL companyDAL = new CompanyDAL();

    public frmController()
    {
        InitializeComponent();
    }

    private void frmController_Load(object sender, EventArgs e)
    {
        backColor = this.BackColor;
        foreColor = this.ForeColor;
        lblCompanyName.Text = GetCompanyName();
        this.Show();
        if (company == null)
        {
            MessageBox.Show(
                "An error has occurred accessing database -\n" +
                " contact system administrator", "Database Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }
        else
        {
            apUser = new AccountProUser();
            Form login = new frmLogin(this);
            login.ShowDialog(this);
            if (apUser != null)
            {
                Form menu = new frmMenu(this);
                nextForm.ShowDialog(this);
            }
            else
            {
                Application.Exit();
            }
        }
    }

    private string GetCompanyName()
    {
        company.FirstName = companyDAL.GetCompanyName;
        return company.FirstName;
    }
}

    public partial class **frmMenu** : Form
{
    private List<apallowablemodules> allowableModules = new List<apallowablemodules>();
    private List<apmodule> lstMenuOptions;

    private Form nextForm = new Form();

    public frmMenu(frmController frmController)
    {
        InitializeComponent();
        this.CenterToParent();
        this.BackColor = frmController.BackColor;
        this.ForeColor = frmController.ForeColor;
        UIMenu uiMenu = new UIMenu();
        lstMenuOptions = uiMenu.GetMenuOptions();
        FlowLayoutPanel flp = uiMenu.SetupMenu(lstMenuOptions, this.ToString(), true, "Main Menu");

        foreach (Button button in flp.Controls.OfType<Button>())
        {
            switch (button.Text)
            {
                case "Accounts Payable":
                    {
                        button.Click += btnAccountsPayable_Click;
                        break;
                    }

                case "Accounts Receivable":
                    {
                        button.Click += btnAccountsReceivable_Click;
                        break;
                    }

                case "General Ledger":
                    {
                        button.Click += btnGeneralLedger_Click;
                        break;
                    }

                case "Inventory Control":
                    {
                        button.Click += btnInventoryControl_Click;
                        break;
                    }

                case "Payroll":
                    {
                        button.Click += btnPayroll_Click;
                        break;
                    }

                case "Sales Analysis":
                    {
                        button.Click += btnSalesAnalysis_Click;
                        break;
                    }

                case "Maintain AccountPro":
                    {
                        button.Click += btnMaintainAccountPro_Click;
                        break;
                    }
            }
        }
        flp.Location = new Point(
            (this.Width - flp.Width) / 2,
            ((this.Height - flp.Height) / 2) - 70);

        this.Controls.Add(flp);
        this.ShowDialog();
    }


    public void btnAccountsPayable_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmAccountsPayableMenu();
        this.Close();
    }

    public void btnAccountsReceivable_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmAccountsReceivableMenu();
        this.Close();
    }

    public void btnGeneralLedger_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmGeneralLedgerMenu();
        this.Close();
    }

    public void btnInventoryControl_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmInventoryControlMenu();
        this.Close();
    }

    public void btnPayroll_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmPayrollMenu();
        this.Close();
    }

    public void btnSalesAnalysis_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmSalesAnalysisMenu();
        this.Close();
    }

    public void btnMaintainAccountPro_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmMaintainAccountProMenu();
        this.Close();
    }

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

    public override string ToString()
    {
        return "MainMenu";
    }
}

public partial class **frmInventoryControlMenu** : Form
{
    private List<apallowablemodules> allowableModules = new List<apallowablemodules>();
    private List<apmodule> lstMenuOptions;

    public frmInventoryControlMenu()
    {
        InitializeComponent();
        this.CenterToParent();
        this.BackColor = frmController.backColor;
        this.ForeColor = frmController.foreColor;
        UIMenu uiMenu = new UIMenu();
        lstMenuOptions = uiMenu.GetMenuOptions("Inventory Control", frmController.apUser);
        FlowLayoutPanel flp = uiMenu.SetupMenu(lstMenuOptions, this.ToString(), false, "Inventory Control Menu");


        foreach (Button button in flp.Controls.OfType<Button>())
        {
            switch (button.Text)
            {
                case "Maintain Inventory":
                    {
                        button.Click += btnMaintainInventory_Click;
                        break;
                    }

                case "Receive Inventory":
                    {
                        button.Click += btnReceiveInventory_Click;
                        break;
                    }

                case "Print Pick Tickets":
                    {
                        button.Click += btnPrintPickTickets_Click;
                        break;
                    }

                case "End Of Period Processing":
                    {
                        button.Click += btnEndOfPeriodProcessing_Click;
                        break;
                    }
            }
        }
        flp.Location = new Point(
            (this.Width - flp.Width) / 2,
            ((this.Height - flp.Height) / 2) - 70);

        this.Controls.Add(flp);
        this.ShowDialog();
    }

    private void btnMaintainInventory_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmMaintainInventory();
        this.Close();
    }

    private void btnReceiveInventory_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmReceiveInventory();
        this.Close();
    }

    private void btnPrintPickTickets_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmPrintPickTickets();
        this.Close();
    }

    private void btnEndOfPeriodProcessing_Click(object sender, EventArgs e)
    {
        frmController.nextForm = new frmEndOfPeriodProcessing();
        this.Close();
    }

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

</apmodule></apallowablemodules></apallowablemodules></apmodule></apallowablemodules></apallowablemodules>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900