在按钮事件中推广表单调用

时间:2015-12-14 06:51:05

标签: c# visual-studio

每当点击一个按钮时,我通常会调用这段代码来显示表单。

    private frmSelection _frmSelection;`

    private void _frmSelection_FormClosing(object sender, FormClosingEventArgs e)
    {
        _frmSelection = null;
    }

    private void changeFeedOrderToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (_frmSelection == null)
        {
            _frmSelection = new frmSelection();
            _frmSelection.FormClosing += _frmSelection_FormClosing;
            _frmSelection.WindowState = FormWindowState.Minimized;
            _frmSelection.Show();
            _frmSelection.WindowState = FormWindowState.Normal;
        }
        else
        {
            _frmSelection.WindowState = FormWindowState.Minimized;
            _frmSelection.WindowState = FormWindowState.Normal;
        }
    }

如果表单已经打开,它将显示已打开的实例,而不是创建新实例。它运作正常。
但我的问题是我需要复制粘贴并在添加新表单时更改表单名称 如何推广和添加 的 辅助  类?

2 个答案:

答案 0 :(得分:2)

这样的事情:

public sealed class ReusableFormContainer<T> : IDisposable
    where T : Form, new()
{
    private bool isDisposed;

    private void HandleFormClosing(object sender, FormClosingEventArgs e)
    {
        Form = null;
    }

    public T Form { get; private set; }

    public void Show()
    {
        if (isDisposed)
        {
            throw new ObjectDisposedException(null);
        }

        if (Form == null)
        {
            Form = new T { WindowState = FormWindowState.Minimized };
            Form.FormClosing += HandleFormClosing;
            Form.Show();
        }
        else
        {
            Form.WindowState = FormWindowState.Minimized;
        }

        Form.WindowState = FormWindowState.Normal;
    }

    public void Dispose()
    {
        // IDisposable.Dispose is implemented to handle cases, when you want to close
        // wrapped form using code
        if (!isDisposed)
        {
            Form?.Dispose();
            isDisposed = true;
        }
    }
}

用法:

// must be initialized somewhere in constructors
private readonly ReusableFormContainer<FormA> container_A;
private readonly ReusableFormContainer<FormB> container_B;

private void button1_Click(object sender, EventArgs e)
{
    container_A.Show();
}

private void button2_Click(object sender, EventArgs e)
{
    container_B.Show();
}

答案 1 :(得分:1)

以下代码可能会执行您想要的操作。只是考虑使用Application函数,因为它们可以涵盖线程上的所有表单。

    public partial class Form1 : Form
{
    public int i; 

    private Form1 _frmSelection;

    public Form1()
    {
        InitializeComponent();
        i = Application.OpenForms.Count;

    }
private void _frmSelection_FormClosing(object sender, FormClosingEventArgs e)
    {
        _frmSelection = null;
    }


    private void button1_Click(object sender, EventArgs e)
    {

        if (_frmSelection == null)
        {

            _frmSelection = new Form1();

            _frmSelection.FormClosing += _frmSelection_FormClosing;
            _frmSelection.WindowState = FormWindowState.Minimized;
            _frmSelection.WindowState = FormWindowState.Normal;
            _frmSelection.Show();

            if (Application.OpenForms.Count > 1)
            {

                _frmSelection.Text = Application.OpenForms[i].Text + " and going";
            }


        }
        else
        {
            _frmSelection.WindowState = FormWindowState.Minimized;
            _frmSelection.WindowState = FormWindowState.Normal;

        }
    }
}