2

我发现我开始对几个不同的点击事件使用相同的代码。我有一个 MDI 表单,并且有我称之为“主子”的“主子”,它们将打开与主关联的其他子。这是正在进行的主要/详细信息。一个例子是公司主子将有按钮来打开与公司关联的联系人、行业等。下面是打开 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
        }
    }
4

1 回答 1

1

要执行自定义函数 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
        }
    }
于 2013-05-08T13:14:15.080 回答