程序重新进入封闭表格 [英] Program reentering closed form

查看:80
本文介绍了程序重新进入封闭表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0

下来投票



最爱











我使用静态表单对象来调用下一个要显示的表单。我有一个控制表单,其中包含调用菜单的静态表单对象。单击菜单选项时,将使用所需表单的名称更新表单对象。控制器然后调用ShowDialog(this)方法。



这可以达到一定程度。



程序的流程是这样的,



控制器调用登录表单创建一个用户对象。



如果登录成功,则调用主菜单表格。



用户单击按钮以选择所需的选项,将相应的子菜单分配给静态表单对象。



当用户单击子菜单上的选项时静态对象已更新。



我使用new关键字将值赋给静态对象,并发出对Close()方法的调用(我试过调用也是Dispose()方法。



控制应该传回控制表,但是主菜单会将用户选择的值重新分配给静态对象,即使它已被关闭。



当我在控制表单中调用ShowDialog(this)方法时,我得到一个无法访问被处理对象的错误。



我们非常感谢任何建议。

解决方案

这是代码



  public   static 表格nextForm; 

private 公司公司= 公司();

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。显示(
访问数据库时发生错误-\ n +
联系系统管理员 数据库错误
MessageBoxButtons.OK,MessageBoxIcon.Error);
Application.Exit();
}
其他
{
apUser = AccountProUser();
表格login = new frmLogin( this );
login.ShowDialog( this );
if (apUser!= null
{
表格菜单= new frmMenu( this );
nextForm.ShowDialog( this );
}
else
{
Application.Exit();
}
}
}

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

public partial class ** frmMenu **:表格
{
私人列表< apallowablemodules> allowableModules = new List< apallowablemodules>();
私人列表< apmodule> lstMenuOptions;

private 表格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 ,< span class =code-string> 主菜单);

foreach (按钮 flp.Controls.OfType< Button>( ))
{
switch (button.Text)
{
case 应付帐款
{
button.Click + = btnAccountsPayable_Click;
break ;
}

case 应收帐款
{
button.Click + = btnAccountsReceivable_Click;
break ;
}

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

case 库存控制
{
button.Click + = btnInventoryControl_Click;
break ;
}

case 工资单
{
button.Click + = btnPayroll_Click;
break ;
}

case 销售分析
{
button.Click + = btnSalesAnalysis_Click;
break ;
}

case 维护AccountPro
{
button.Click + = btnMaintainAccountPro_Click;
break ;
}
}
}
flp.Location = 点(
this .Width - flp.Width)/ 2
((这个 .Height - flp.Height)/ 2 ) - 70 );

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


public void btnAccountsPayable_Click(< span class =code-keyword> 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 覆盖 string ToString()
{
return MainMenu;
}
}

public partial class ** frmInventoryControlMenu **:表格
{
private 列表< apallowablemodules> allowableModules = new List< apallowablemodules>();
私人列表< 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 ,< span class =code-string> 库存控制菜单);


foreach (按钮 flp.Controls.OfType< ;按钮>())
{
开关(button.Text)
{
case 维护库存
{
按钮。点击+ = btnMaintainInventory_Click;
break ;
}

case 接收库存
{
button.Click + = btnReceiveInventory_Click;
break ;
}

case 打印选择门票
{
button.Click + = btnPrintPickTickets_Click;
break ;
}

case 期末处理
{
button.Click + = btnEndOfPeriodProcessing_Click;
break ;
}
}
}
flp.Location = 点(
this .Width - flp.Width)/ 2
((这个 .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 >


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.

解决方案

Here is the code

    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>


这篇关于程序重新进入封闭表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆