private void ListViewGeneric_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
Run Code Online (Sandbox Code Playgroud)
并将此函数指定为ListView1,ListView2,ListView3的事件函数:
this.ListView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.ListViewGeneric_DragEnter)
this.ListView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.ListViewGeneric_DragEnter)
this.ListView3.DragEnter += new System.Windows.Forms.DragEventHandler(this.ListViewGeneric_DragEnter)
Run Code Online (Sandbox Code Playgroud)
private void ListViewGeneric_DragDrop(object sender, DragEventArgs e)
{
ListView listView = sender as ListView;
System.Drawing.Point cp = listView.PointToClient(new System.Drawing.Point(e.X, e.Y));
ListViewItem dragToItem = listView.GetItemAt(cp.X, cp.Y);
if (dragToItem != null)
{
int dropIndex = dragToItem.Index;
MoveListItem(listView, listView.FocusedItem.Index, dropIndex);
}
}
Run Code Online (Sandbox Code Playgroud)
并做同样的事情DragDropEventHandler?
在这种情况下,我可以sender用来知道哪个控件称为函数.这是个坏主意吗?有什么东西会变坏,或变得不可预测,模糊不清?
Ben*_*igt 11
那很好.
你不想做的就是开始
if (sender == control1) { ... }
else if (sender == control2) { ... }
...
Run Code Online (Sandbox Code Playgroud)
以适当的方式命名处理程序以指示处理多个控件的事件也很重要(你已经做得很好).