Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要将子窗体按钮单击统一到一个例程中_C#_Winforms - Fatal编程技术网

C# 需要将子窗体按钮单击统一到一个例程中

C# 需要将子窗体按钮单击统一到一个例程中,c#,winforms,C#,Winforms,我发现我开始对一些不同的点击事件使用相同的代码。我有一个MDI表单,有我称之为“主控子对象”的子对象,它们将打开与主对象关联的其他子对象。这是正在进行的主要/详细的事情。例如,公司主控子系统将具有按钮,用于打开与公司关联的联系人、行业等。下面是打开联系人子窗体的代码示例。此代码也用于其他代码 我想做的是能够只使用一个按钮,填写按钮、表单、消息和公司与联系人之间的连接标签。到目前为止,botton的代码就是我所拥有的,我用我正在寻找的东西标记了需要更改的行。带有单箭头的线条“似乎”起作用,但多箭头

我发现我开始对一些不同的点击事件使用相同的代码。我有一个MDI表单,有我称之为“主控子对象”的子对象,它们将打开与主对象关联的其他子对象。这是正在进行的主要/详细的事情。例如,公司主控子系统将具有按钮,用于打开与公司关联的联系人、行业等。下面是打开联系人子窗体的代码示例。此代码也用于其他代码

我想做的是能够只使用一个按钮,填写按钮、表单、消息和公司与联系人之间的连接标签。到目前为止,botton的代码就是我所拥有的,我用我正在寻找的东西标记了需要更改的行。带有单箭头的线条“似乎”起作用,但多箭头的线条无法使其正确。出于比较的原因,两者都提供

有人能仔细看看这个/这些,看看我在合并代码中做错了什么(或遗漏了什么)

谢谢……约翰

//打开联系人子窗体的代码

        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,需要从表单创建派生类 并从该派生类创建所有表单

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

你只是想在所有表单中重复使用同一段代码吗?因为现在它只是在一个主控子表单中,它将有6个其他子表单打开以显示数据。但既然你提到了这一点,其他人可能会有一些相似之处。我仍然不确定这个问题。但是一个猜测是,继承能解决你的问题吗??我的意思是创建一个具有公共功能的类,并从中派生其余的类。您的公共类甚至可以包含按钮及其事件。那么发布的是什么呢?是否可以对其进行修改,以便能够从不同的按钮管理表单的打开?原因是,一种本能的感觉,实际上,是在新年开始后,可能会有一个“变化”来作为什么是需要的。