需要将子表单按钮点击统一到一个例程中

本文关键字:一个 例程 表单 按钮 | 更新日期: 2023-09-27 17:49:20

我发现我开始对几个不同的单击事件使用相同的代码。我有一个MDI表单,有"master子",我称之为"master子",它们将打开与master关联的其他子窗体。这是主/细节的关系。例如,Company主子节点将具有打开与Company关联的Contact、Industry等的按钮。下面是打开Contact子表单的代码示例。此代码也被用于其他代码。

我希望做的是能够只使用一个,并填写按钮,表单,消息,以及公司和联系人之间的连接标签。在底部的代码是什么,我已经到目前为止,我标记的行,需要改变什么,我正在寻找。单箭头线"似乎"工作,但多箭头线不能得到它的权利。为了比较,两者都提供

有人可以看看这个/这些,看看我在合并代码中做错了什么(或缺失)?

谢谢…约翰

//打开联系人子表单代码

        private void btnCompanyContact_Click(object sender, EventArgs e)
    {
        bool isOpen = false;
        foreach (Form f in Application.OpenForms)
        {
            if (f is frmContact)
            {
                isOpen = true;
                MessageBox.Show("The Contact list is already open.", "INFORMATION", MessageBoxButtons.OK);
                f.BringToFront();
                f.Controls["lblRecordID"].Text = lblCompanyID.Text;
                break;
            }
        }
        if (!isOpen)
        {
            frmContact contact = new frmContact();
            contact.MdiParent = this.MdiParent;
            contact.ReceiveValue(lblCompanyID.Text);
            contact.StartPosition = FormStartPosition.Manual;
            contact.Location = new Point(100, 100);
            contact.Show();
        }
        else
        {
            //do nothing
        }
    }

//合并所有按钮打开到这个例程

        private void OpenCompanyInformationForm(Button btn, Form name, string message, string lbl)
    {
        bool isOpen = false;
        foreach (Form f in Application.OpenForms)
        {
  ->        if (f == name)
            {
                isOpen = true;
  ->            MessageBox.Show("The " + message + " list is already open.", "INFORMATION", MessageBoxButtons.OK);
                f.BringToFront();
  ->            f.Controls[lbl].Text = lblCompanyID.Text;
                break;
            }
        }
        if (!isOpen)
        {
   ->->->   frmContact contact = new frmContact();
            contact.MdiParent = this.MdiParent;
            contact.ReceiveValue(lblCompanyID.Text);
            contact.StartPosition = FormStartPosition.Manual;
            contact.Location = new Point(100, 100);
            contact.Show();
        }
        else
        {
            //do nothing
        }
    }

需要将子表单按钮点击统一到一个例程中

要执行自定义函数ReceiveValue,您需要从Form创建一个派生类并从这个派生类

创建所有表单
public class ContactBase : Form
{
    public void ReceiveValue(string p_Value)
    {
        Button button = (Button)this.Controls["lblRecordID"];
        if (button == null) return;
        button.Text = p_Value;
    }
}

private void OpenCompanyInformationForm(Form name)
    {
        bool isOpen = false;
        foreach (Form f in Application.OpenForms)
        {
            // Just to compare, you can use the Name property
  ->        if (f.Name == name.Name)
            {
                isOpen = true;
                // If the message is just a name of form, you can use Name or Text property
                // in this case you can supress message param
  ->            MessageBox.Show("The " + f.Text + " list is already open.", "INFORMATION", MessageBoxButtons.OK);
                f.BringToFront();
                // If the ReceiveValue is just to pass the text of lblCompanyID for lblRecordID button, you can use the function here
  ->            ((ContactBase)name).ReceiveValue(lblCompanyID.Text);
                break;
            }
        }
        if (!isOpen)
        {
   ->->->   ContactBase contact = (ContactBase)Activator.CreateInstance(name.GetType());
            contact.MdiParent = this.MdiParent;
            contact.ReceiveValue(lblCompanyID.Text);
            contact.StartPosition = FormStartPosition.Manual;
            contact.Location = new Point(100, 100);
            contact.Show();
        }
        else
        {
            //do nothing
        }
    }