简体   繁体   English

如何为多个类似控件编写事件处理程序?

[英]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.

相关问题 如何将相似的事件处理程序添加到多种形式? IE按键事件到多个文本框 - How to add similar event handlers to multiple forms? I.E. keypressed event to multiple textboxes 将通用事件处理程序连接到同一类型的多个控件 - Hooking up generic event handlers to multiple controls of the same type C#WinForm多次单击事件处理程序,用于类似功能 - C# WinForm multiple click event handlers for similar function 如何在XAML中为附加事件添加多个处理程序? - How to add multiple handlers to an attached event in XAML? 将事件处理程序附加到数据模板中的控件 - Attach event handlers to controls within a data template 嵌套动态控件,使用自定义事件处理程序 - Nested Dynamic Controls, using Custom Event Handlers 有没有一种方法可以在表单上动态复制控件及其事件处理程序? - Is there a way to dynamically copy controls and their event handlers on a form? 动态地将事件处理程序添加到面板中的控件 - Adding event handlers Dynamically to controls within a panel WPF:如何为具有相同动画和不同事件处理程序的多个控件有效地编写故事板? - WPF: How to write efficiently a storyboard for multiple controls with the same animation and different event handler for `completed`? 如何使用.ashx处理程序发送控件 - How to send controls with .ashx Handlers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM