您可以将“GotFocus”事件分配给这样的方法,并以这种方式记录“最后聚焦”控件。然后在您的 SelectAll_CLick 处理程序中,如果分配了列表视图,则选择全部,否则 - 不要!
private ListView mLastSelectedListView;
private void ListViews_GotFocus(object sender, EventArgs e)'
{
ListView lv = sender as ListView;
if (null == lv) return;
mLastSelectedListView = lv;
}
private void SelectAll_Click(object sender, EventArgs e)
{
if (null == mLastSelectedListView) return;
mLastSelectedListView.SelectAll();
}
这是支持上述内容的快速“SelectAll”扩展方法;
public static class ListViewExtensions
{
public static void SelectAll(this ListView lv)
{
foreach (ListViewItem item in lv.Items)
item.Selected = true;
}
}