所以我点击了这三个按钮。
private void btn_OpenNotePad_Click(object sender, EventArgs e)
{
this.Hide();
Form2 forma_NotePad = new Form2();
forma_NotePad.ShowDialog();
this.Close();
}
private void btn_OpenPaint_Click(object sender, EventArgs e)
{
this.Hide();
Form3 forma_Paint = new Form3();
forma_Paint.ShowDialog();
this.Close();
}
private void btn_ContactAdmin_Click(object sender, EventArgs e)
{
this.Hide();
Form4 forma_ContactAdmin = new Form4();
forma_ContactAdmin.ShowDialog();
this.Close();
}
它们都非常相似,所以我想可以为它们创建一种方法吗?类似的东西:
private void OpenForm(parameter1, parameter2)
{
this.Hide();
parameter1 parameter2 = new parameter1();
parameter2.ShowDialog();
this.Close();
}
所以我可以打电话给他们
OpenForm(parameter1,parameter2);
让我的代码更简洁,更清洁。
答案 0 :(得分:6)
当然,您可以使用单独的方法重构此功能:
private void btn_OpenNotePad_Click(object sender, EventArgs e)
{
Open<Form2>();
}
private void btn_OpenPaint_Click(object sender, EventArgs e)
{
Open<Form3>();
}
private void btn_ContactAdmin_Click(object sender, EventArgs e)
{
Open<Form4>();
}
private void Open<TForm>() where TForm: Form, new()
{
this.Hide();
new TForm().ShowDialog();
this.Close();
}