拖放操作,DragDrop,DragLeave,DragEnter等事件何时使用?
很多控件都有这些事件,但是一直没有使用过,不知道这些事件主要用于那些方面,请指教,谢谢 问题点数:20、回复次数:4Top
1 楼mfjustlove()回复于 2006-12-02 13:01:00 得分 0
文件的拖拽,ListBox中项的拖拽(可以多个之间进行),TreeView中项的多拽(如资源管理器的左侧树目录),ListView项的拖拽(如Windows中文件的拖放操作)Top
2 楼mfjustlove()回复于 2006-12-02 13:05:00 得分 0
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref17/html/E_System_Windows_Forms_Control_DragOver.htm#codeExampleToggle
msdn中有示例Top
3 楼SmallMummy(爬山真累人~!)回复于 2006-12-02 15:06:22 得分 20
如果用户移出一个窗口,则引发 DragLeave 事件。
如果鼠标进入另一个控件,则引发该控件的 DragEnter。
如果鼠标移动但停留在同一个控件中,则引发 DragOver 事件。
在完成拖放操作时发生DragDrop事件.
贴个msdn的代码:
下面的代码示例演示如何使用 DragDropEffects 枚举指定数据应如何在拖放操作中涉及到的控件之间进行传送。此示例要求您的窗体中包括一个 RichTextBox 控件和一个 Label 控件,并且 Label 控件用有效文件名列表进行填充。当用户将文件名拖到 RichTextBox 控件上时,该控件的 DragEnter 事件被引发。在事件处理程序中,DragEventArgs 的 Effect 属性被初始化为 DragDropEffects,以便指示该文件路径所引用的数据应复制到 RichTextBox 控件中。
private void Form1_Load(object sender, EventArgs e)
{
// Sets the AllowDrop property so that data can be dragged onto the control.
richTextBox1.AllowDrop = true;
// Add code here to populate the ListBox1 with paths to text files.
}
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Determines which item was selected.
ListBox lb =( (ListBox)sender);
Point pt = new Point(e.X,e.Y);
int index = lb.IndexFromPoint(pt);
// Starts a drag-and-drop operation with that item.
if(index>=0)
{
lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Link);
}
}
private void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
// If the data is text, copy the data to the RichTextBox control.
if(e.Data.GetDataPresent("Text"))
e.Effect = DragDropEffects.Copy;
}
private void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
// Loads the file into the control.
richTextBox1.LoadFile((String)e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText);
}
Top
4 楼bigrongshu(Life is full of possibilities)回复于 2006-12-03 12:04:31 得分 0
DragDrop - 拖动开始/完成时发生
DragLeave - 拖动离开该控件时发生
DragEnter - 拖动进入该控件时发生Top




