[英]How can I refactor this C# code below?
我的Form1中有12个按钮,每个按钮旁边都有一个文本框。 button事件调用一个名为dialogOpen的方法,该方法处理从form2获取对象并将字符串值放置在文本框中的情况。
如何根据用户单击的按钮将返回的值放在文本框中? 因此,如果是用户单击的是button1,则应将返回的文本放置在textbox1中;如果是用户单击的是button2,则应将返回的文本放置在textbox2中。 重点是避免使用字符串名称进行检查,因为所有按钮都可以称为“浏览”。
现在我下面的代码可以做到这一点,但它是重复性的,这样做有更好的选择吗?
private void dailogueOpen(String btnName)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
switch (btnName)
{
case "btn1":
textBox1.Text = form2result.getValue();
break;
case "btn2":
textBox2.Text = form2result.getValue();
break;
case "btn3":
textBox3.Text = form2result.getValue();
break;
case "btn4":
textBox4.Text = form2result.getValue();
break;
case "btn5":
textBox5.Text = form2result.getValue();
break;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
String name = "btn1";
dailogueOpen(name);
}
private void button2_Click(object sender, EventArgs e)
{
String name = "btn2";
dailogueOpen(name);
}
private void button3_Click(object sender, EventArgs e)
{
String name = "btn3";
dailogueOpen(name);
}
private void button4_Click(object sender, EventArgs e)
{
String name = "btn4";
dailogueOpen(name);
}
private void button5_Click(object sender, EventArgs e)
{
String name = "btn5";
dailogueOpen(name);
}
编辑:我只是注意到您的事件处理程序。 随之而来的是更多的重构:
就在这里。 您需要以某种方式将文本框关联到按钮。 例如,创建一个像这样的字典:
Dictionary<Button, TextBox> _dict;
_dict[button1] = textBox1;
_dict[button2] = textBox2;
...
对所有事件使用一个事件处理程序:
private void button_click(object sender, EventArgs e)
{
dialogeOpen((Button)sender);
}
更改dialogueOpen
以接受按钮而不是字符串并
_dict[btn].Text = form2Result.getValue();
1您在所有button
上使用same delegate
Nota (Thank's to Marty) :
在表单设计器中时,选择所有按钮,然后为所有按钮依次单击“ Generic_Click”,或者可以使用下面的代码。
this.btn1.Click += new System.EventHandler(Generic_Click); //the same delegate
this.btn2.Click += new System.EventHandler(Generic_Click);
this.btn3.Click += new System.EventHandler(Generic_Click);
....
private void Generic_Click(object sender, EventArgs e)
{
var control = (Button)sender;
if( control.Name == "btn1")
{
....
}
else if( control.Name == "btn2")
{
....
}
else if( control.Name == "btn3")
{
....
}
}
替换您的事件处理程序以
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
String name = button.Text;// Tag, name etc
dailogueOpen(name);
}
我首先只对按钮使用一个事件处理程序,如下所示:
protected void ButtonClick(object sender, EventArgs e)
{
Button clickedButton = (Button) sender;
string selectedId = clickedButton.ID;
string[] idParameters = selectedId.Split('_');
string textBoxId = "textbox" + idParameters[1];
dailogueOpen(textBoxId);
}
我在这里所做的是使用一种模式来命名文本框,例如,如果您具有ID为:button_1,button_2,...,button_n的按钮,则可以推断出相应的文本框是什么。
如果单击button_1,则通过拆分其ID将知道其对应的文本框是ID为textbox1的文本框。
然后,对话框打开函数将如下所示:
private void dailogueOpen(string textBoxId)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
TextBox textBox = (TextBox)this.Form.FindControl("MainContent").FindControl(textBoxId);
textBox.Text = resulOfForm2.getValue();
}
}
其中MainContent是文本框所在的容器的ID。
总而言之:
您可以在所有按钮单击上使用字典和一种事件方法
Dictionary<Button, TextBox> dx = new Dictionary<Button, TextBox>;
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
dx[button].Text = form2result.getValue();
}
和这样的构造函数:
public ClassName()
{
dx.Add(button1, textBox1);
dx.Add(button2, textBox2);
dx.Add(button3, textBox3);
}
我认为您可以做的第一件事是通过消除对switch语句的需要来提高可读性:
private void dailogueOpen(TextBox textBox)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
textBox.Text = form2result.getValue();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
dailogueOpen(textBox1);
}
private void button2_Click(object sender, EventArgs e)
{
dailogueOpen(textBox2);
}
private void button3_Click(object sender, EventArgs e)
{
dailogueOpen(textBox3);
}
private void button4_Click(object sender, EventArgs e)
{
dailogueOpen(textBox4);
}
private void button5_Click(object sender, EventArgs e)
{
dailogueOpen(textBox5);
}
然后,这为您提供了一个合理的方法签名,以引入字典(由其他两个人建议)用于将Button映射到TextBox(由其他两个人建议)针对所有按钮使用字典(由另外两个人建议)。
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null) return;
String name = button.Text;// Tag, name etc
dailogueOpen(name);
}
private void dailogueOpen(String btnName)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
SetTxt(btnName,form2result.getValue());
}
}
}
private void SetTxt(string btnName, string value)
{
int lenght = "Button".Length;
string index = btnName.Substring(lenght); //remove Button
TextBox t = (TextBox)this.Controls.Find("textBox" + index, true)[0];
if (t != null)
t.Text = value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.