为不同的Windows窗体控件使用相同的事件处理程序是否可以安全? [英] Is it safe to use the same event handler for different Windows Forms Controls?
问题描述
我有3个DataGridView,我想有一个工具,允许用户通过点击列标题对数据进行排序。我可以为每个这些DataGridView的ColumnHeaderMouseClick事件处理一个事件处理程序,但是我做了一个:
private void dataGridView_ColumnHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
SortDataGridView((发送方为DataGridView),e.ColumnIndex);
}
SortDataGridView如下所示:
///< summary>
///通过其columnIndex给出的列对给定的DataGridView进行排序。
///默认排序(如果当前未排序)升序。如果是
///已经排序升序,请将其更改为降序。如果它是降序,
///将其更改为升序。
///< / summary>
///< param name =dataGridViewToSort> DataGridViewToSort< / param>
///< param name =columnIndexToSortBy>我们要在DataGridView中排序的列的索引。< / param>
private void SortDataGridView(DataGridView dataGridViewToSort,int columnIndexToSortBy)
{
开关(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;
默认值:
break;
}
}
每个DataGridView的ColumnHeaderMouseClick事件都被连接到这个处理程序。这意味着为了实现哪一个在运行时引发事件,我不得不说(发件人为DataGridView)。这是安全吗?发送者可能不是DataGridView吗?
我认为这是非常可以接受的,我经常使用这些控件类似的功能。您可以在事件中添加检查,以确保发件人是datagrid类型,如果您担心发件人是其他对象。
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. I could've had an event handler for the ColumnHeaderMouseClick event for each of these DataGridView's, but instead I made one:
private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
SortDataGridView((sender as DataGridView), e.ColumnIndex);
}
SortDataGridView looks like this:
/// <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. This implies that in order to realise which one raised the event at runtime, I have to say (sender as DataGridView). Is this safe? Could sender ever be something that's not a 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.
这篇关于为不同的Windows窗体控件使用相同的事件处理程序是否可以安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!