如何从Collection

时间:2015-09-08 00:23:26

标签: c# mvp

我有一个包含多个tabPages的程序,每个tabPages都有一个附加的表单,我希望在用户点击tabpage页面时动态加载表单,当用户点击另一个tabpage我想要使用的表单时发布表单内存并在内存中加载新表单,我正在尝试这个,但我无法让它工作:

public void On_SelectedTabChange()
    {
        switch (this._view.TabSeleccionado)
        {
            case "_tabDeposito":
                LoadModule("DepoMainForm");
                break;
            case "_tabProduccion":
                MessageBox.Show("Modulo no implementado");
                break;
        }
    }

    public void LoadModule(string module)
    {
        this._view.OpenForm(module);
    }

----------------(查看/表格)-------------------

public void Attach(IMainCallbacks callback)
    {
        this._tabControl1.SelectedIndexChanged +=
            (objSender, e) => callback.On_SelectedTabChange();
    }

    public void OpenForm(string formName)
    {
        switch (formName)
        {
            case "DepoMainForm":
                var forms = Application.OpenForms;
                foreach (var form in forms)//WRONG
                {
                    form.dispose();
                }                  
                DepoMainForm DepoMainForm = new DepoMainForm
                {
                    TopLevel = false,
                    Dock = DockStyle.Fill,
                    Parent = _tabDeposito,
                    Visible = true,
                    FormBorderStyle = FormBorderStyle.None
                };
                this._tabDeposito.Controls.Add(DepoMainForm);
                var presenter = new DepoMainPresenter(DepoMainForm);
                presenter.Init();                     
                break;

该事件,以及#34; DepoMainForm"表单是实例化并附加到tabpage,在另一种情况下MessageBos相应地显示,但我不知道如何处理除用户更改标签页时加载的表单以外的所有表单。

1 个答案:

答案 0 :(得分:0)

好的,用这个解决了:

 public void OpenForm(string formName)
    {
        switch (formName)
        {
            case "DepoMainForm":
                DisposeForms();
                DepoMainForm DepoMainForm = new DepoMainForm
                {
                    TopLevel = false,
                    Dock = DockStyle.Fill,
                    Parent = _tabDeposito,
                    Visible = true,
                    FormBorderStyle = FormBorderStyle.None
                };
                this._tabDeposito.Controls.Add(DepoMainForm);
                var presenter = new DepoMainPresenter(DepoMainForm);
                presenter.Init();

                break;

        }

    }

    public void DisposeForms()
    {
        List<Form> toBeDisposed = new List<Form>();
        foreach (Form form in Application.OpenForms)
        {
            if(form.Tag != null && form.Tag.ToString() != "MainForm")
            toBeDisposed.Add(form);
        }
        foreach (Form form in toBeDisposed)
        {
            form.Dispose();
        }
    }