简体   繁体   English

为不同的Windows窗体控件使用相同的事件处理程序是否安全?

[英]Is it safe to use the same event handler for different Windows Forms Controls?

Is this good practise? 这是好习惯吗? I have 3 DataGridView's and I want to have a facility that allows a user to sort the data by clicking on a column header. 我有3个DataGridView,我希望有一个工具允许用户通过单击列标题对数据进行排序。 I could've had an event handler for the ColumnHeaderMouseClick event for each of these DataGridView's, but instead I made one: 我可以为每个DataGridView的ColumnHeaderMouseClick事件创建一个事件处理程序,但我做了一个:

private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        SortDataGridView((sender as DataGridView), e.ColumnIndex);
    }

SortDataGridView looks like this: SortDataGridView看起来像这样:

/// <summary>
    /// Sorts a given DataGridView by a column given by its columnIndex.
    /// Default sort (if it isn't currently sorted) is Ascending. If it's 
    /// already sorted Ascending, change it to Descending. If it is Descending,
    /// change it to Ascending.
    /// </summary>
    /// <param name="dataGridViewToSort">The DataGridViewToSort</param>
    /// <param name="columnIndexToSortBy">The index of the column which we want to sort by in the DataGridView.</param>
    private void SortDataGridView(DataGridView dataGridViewToSort, int columnIndexToSortBy)
    {
        switch (dataGridViewToSort.SortOrder)
        {
            case SortOrder.Ascending:
                dataGridViewToSort.Sort(dataGridViewToSort.Columns[columnIndexToSortBy], ListSortDirection.Descending);
                break;
            case SortOrder.Descending:
                dataGridViewToSort.Sort(dataGridViewToSort.Columns[columnIndexToSortBy], ListSortDirection.Ascending);
                break;
            case SortOrder.None:
                dataGridViewToSort.Sort(dataGridViewToSort.Columns[columnIndexToSortBy], ListSortDirection.Ascending);
                break;
            default:
                break;
        }
    }

Each of the DataGridView's ColumnHeaderMouseClick event is hooked up to this handler. 每个DataGridView的ColumnHeaderMouseClick事件都连接到此处理程序。 This implies that in order to realise which one raised the event at runtime, I have to say (sender as DataGridView). 这意味着为了实现哪一个在运行时引发事件,我不得不说(sender作为DataGridView)。 Is this safe? 这样安全吗? Could sender ever be something that's not a DataGridView? 发件人可能是不是DataGridView的东西吗?

它不仅好,而且更好,因为它使您的代码更易于重用,可维护和可扩展。

I think this is very acceptable and I use this often for controls that have similar functionality. 我认为这是非常可以接受的,我经常将它用于具有类似功能的控件。 You can add a check in the event to make sure that sender is of the datagrid type if you are concerned with sender being some other object. 如果您担心发件人是其他对象,则可以在事件中添加检查以确保发件人属于数据网格类型。

这是一个很好的做法,遵循DRY(不要重复自己)的原则

If you wanted to be safe you could always check the Type of the sender before attempting to do anything with it. 如果您想要安全,您可以在尝试使用它之前检查发件人的类型。

In theory you shouldn't be setting the handler of any other objects "ColumnHeaderMouseClick" to this so this shouldn't be necessary. 理论上,您不应该将任何其他对象的处理程序“ColumnHeaderMouseClick”设置为此,因此这不是必需的。

As long as you write the handler to properly handle each control... which is best when the exact same thing needs to be done on multiple controls. 只要你编写处理程序来正确处理每个控件...这对于多个控件需要完全相同的事情是最好的。

Basically as long as it works, and will be easy to maintain you should be good. 基本上只要它有效,并且易于维护你应该是好的。

It's fine and safe. 这很好,很安全。 A small correction: you would check for which one raised the event by 一个小小的修正:你会检查哪一个引发了事件

if (sender == dataGrid1)

etc. But if you have specific functionality that requires knowing which one raised the event then you should probably refactor to individual handlers. 但是如果你有特定的功能需要知道哪一个引发了事件,那么你应该重构个别处理程序。

This solution is absolutely fine in my opinion. 在我看来,这个解决方案绝对没问题。 You saved two event handlers, that would actually do exactly the same stuff. 你保存了两个事件处理程序,实际上会完全相同的东西。 If there were different thins to do in each of the handlers, you shouldn't have done it that way, but here it's saving development and maintaining time. 如果在每个处理程序中都有不同的做法,那么你不应该这样做,但是这样可以节省开发并保持时间。

As soon as one of the DataGridViews has a specific job to do on the event handler you should give it an Event Handler for it's own. 只要其中一个DataGridViews在事件处理程序上有特定的工作,就应该为它自己提供一个事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF如何将同一事件处理程序绑定到两个不同的控件 - WPF How to bind the same event handler to two different controls 使用不同的事件处理程序委托为多个控件实现单个事件处理程序 - Implementing a single event handler for multiple controls with different event handler delegates 在不同的事件处理程序中引用以编程方式添加的控件? - referencing programatically added controls in a different event handler? 对于不同的控件使用与事件相同的功能是不是一个坏主意? - Is it a bad idea to use the same function as event for different controls? WPF:如何为具有相同动画和不同事件处理程序的多个控件有效地编写故事板? - WPF: How to write efficiently a storyboard for multiple controls with the same animation and different event handler for `completed`? 用于关闭Windows Forms应用程序的事件处理程序 - Event handler used to close a Windows Forms application keypress事件处理程序,但在没有表单的Windows应用程序上 - keypress event handler but on a windows app with no forms Windows窗体中的通用事件处理程序的解决方法 - Workaround for generic event handler in Windows Forms 不同 Windows Forms 上的相同计算 - Same Calculation on Different Windows Forms ASP.NET Web窗体:对来自不同命名空间的Web控件使用相同的tagPrefix - ASP.NET Web Forms: Use same tagPrefix for web controls from different namespaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM