简体   繁体   English

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

[英]Generalizing the form calling in a button event

I usually call this piece of code to show a form whenever a button is clicked. 我通常会在单击按钮时调用此代码来显示表单。

    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;
        }
    }

If the form is already open it will show the already opened instance instead of creating new instance. 如果表单已经打开,它将显示已打开的实例,而不是创建新实例。 It is working fine. 它工作正常。
But my problem is i need to copy paste and change the form name whenever i am adding a new form. 但我的问题是我需要复制粘贴并在添加新表单时更改表单名称。
How it can be generalized and added to Helper class? 如何推广并添加到Helper类?

Something like this: 像这样的东西:

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;
        }
    }
}

Usage: 用法:

// 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();
}

The following code might do what you want. 以下代码可能会执行您想要的操作。 Just thought about to use the Application functions because those can cover all forms that are on thread. 只是考虑使用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;

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM