[英]How to write event handlers for multiple similar controls?
Basically I have certain controls where they do similar things but for different controls using different values. 基本上,我有某些控件在其中执行相似的操作,但对于使用不同值的不同控件。 For instance:
例如:
public static void DeleteItemsFromList ( object sender, EventArgs e )
{
ListBox control = null;
switch ( ( ( Button ) sender ).Name )
{
case "EffectsRemove": control = ( ListBox ) ActiveForm [ "EffectsList" ]; break;
case "LayersRemove": control = ( ListBox ) ActiveForm [ "LayersList" ]; break;
case "ObjectsRemove": control = ( ListBox ) ActiveForm [ "ObjectsList" ]; break;
}
control.Items.Add ( ( ( Button ) sender ).Name )
string action = null;
switch ( ( ( CheckButton ) sender ).Name )
{
case "EffectsRemove": action = "Effects"; break;
case "LayersRemove": action = "Layers"; break;
case "ObjectsRemove": action = "Objects"; break;
}
var selectedItem = control.SelectedItem;
if ( selectedItem == null )
return;
Refresh = false;
UpdateUI ( action );
Refresh = true;
}
Is this bad practice? 这是不好的做法吗? Is there a better way to do these kinds of variable event handlers based on similarly behaving controls?
是否有更好的方法基于类似行为的控件来执行此类可变事件处理程序?
Personally I find your example leaves too many opportunities for error. 我个人认为您的示例留下了太多的出错机会。 Your best bet would be to extract the common functionality to a separate method.
最好的选择是将常用功能提取到单独的方法中。
public static void EffectsRemove_Click(object sender, EventArgs e)
{
DeleteItemsFromList(
(Button)sender,
(ListBox)ActiveForm["EffectsList"],
"Effects");
}
public static void LayersRemove_Click(object sender, EventArgs e)
{
DeleteItemsFromList(
(Button)sender,
(ListBox)ActiveForm["LayersList"],
"Layers");
}
public static void ObjectsRemove_Click(object sender, EventArgs e)
{
DeleteItemsFromList(
(Button)sender,
(ListBox)ActiveForm["ObjectsList"],
"Objects");
}
public static void DeleteItemsFromList(
Button sender,
ListBox control,
string action)
{
control.Items.Add(sender.Name);
var selectedItem = control.SelectedItem;
if ( selectedItem == null )
return;
Refresh = false;
UpdateUI action;
Refresh = true;
}
我可以看到您想要重用逻辑的愿望,但是在我看来,这种使代码更脆弱且更难以维护的方法,在这种情况下,即使代码是伪相似的,我可能也希望使用单独的偶数处理程序。
Here's another approach you could use via some delegates defined while subscribing to the EventHandler. 这是您可以通过在订阅EventHandler时定义的一些委托使用的另一种方法。 I think it's a bit easier to read this way, but could get out of hand if you added several other conditions (like if RichTextBox, if Combobox, etc.)
我认为用这种方式读起来要容易一些,但是如果添加了其他几个条件(例如RichTextBox,Combobox等),可能会失控。
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(delegate { DoButtonProcessing(this, "EffectsList", null); });
button2.Click += new EventHandler(delegate { DoButtonProcessing(this, "LayersList", null); });
button3.Click += new EventHandler(delegate { DoButtonProcessing(this, null, "Effects"); });
button4.Click += new EventHandler(delegate { DoButtonProcessing(this, null, "Layers"); });
}
void DoButtonProcessing(object sender, string list, string action)
{
ListBox control = (ListBox)ActiveForm[list]; //can be null here, but your source also allowed that so I assume it's just a snippit.
control.Items.Add(((Button)sender).Name);
var selectedItem = control.SelectedItem;
if (selectedItem == null) return;
Refresh = false;
UpdateUI null;
Refresh = true;
}
You may want to consider using a custom control, where you can derive from Button, and add a few properties to each instance of your CustomButton, representing the string and ListBox that you care about. 您可能要考虑使用自定义控件,可以从Button派生该控件,并向CustomButton的每个实例添加一些属性,以表示您关心的字符串和ListBox。 Then you can tie each button to the same event handler, and act on the properties of the CustomButton, without caring about which one it is.
然后,您可以将每个按钮绑定到相同的事件处理程序,并对CustomButton的属性进行操作,而不必关心它是哪个按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.