我如何将一个事件绑定到多个列表框

how do i tie an event to multiple listboxes

我有一段代码我想用于多个列表框。基本上鼠标在哪里,select那个索引

driversListBox.SelectedIndex = driversListBox.IndexFromPoint(e.X, e.Y);

我该如何编写才能将此代码绑定到 2-3 个列表框并且该代码适用于发件人是谁?

我试过像下面这样投射但没有成功。

(Listbox)sender.IndexFromPoint(e.X, e.Y);

任何帮助将不胜感激

首先将所有列表挂钩到同一个事件:

list1.MouseMove += CheckMove;
list2.MouseMove += CheckMove;
//...
listN.MouseMove += CheckMove;

然后在事件处理程序上:

var currentList = sender as ListBox;
//Now you can use currentList as it points to the list 
//which fired the event.