我发现我开始对几个不同的点击事件使用相同的代码。我有一个MDI表格,并且有#34;主要的孩子"因为我打电话给他们会打开与主人有关的其他孩子。这是正在进行的主要/细节事情。例如,公司主人将有按钮打开与公司关联的联系人,行业等。下面是打开Contact子表单的代码示例。此代码也可用于其他代码。
我要做的是只能使用一个并填写公司和联系人之间的按钮,表单,消息和连接标签。 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
}
}
答案 0 :(得分:1)
要执行自定义函数Receive Value,您需要从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
}
}