拖放操作,DragDrop,DragLeave,DragEnter等事件何时使用?

mz1867 2006-12-02 12:40:03
很多控件都有这些事件,但是一直没有使用过,不知道这些事件主要用于那些方面,请指教,谢谢
...全文
1388 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigrongshu 2006-12-03
  • 打赏
  • 举报
回复
DragDrop - 拖动开始/完成时发生

DragLeave - 拖动离开该控件时发生

DragEnter - 拖动进入该控件时发生
SmallMummy 2006-12-02
  • 打赏
  • 举报
回复
如果用户移出一个窗口,则引发 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);
}


mfjustlove 2006-12-02
  • 打赏
  • 举报
回复
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref17/html/E_System_Windows_Forms_Control_DragOver.htm#codeExampleToggle

msdn中有示例
mfjustlove 2006-12-02
  • 打赏
  • 举报
回复
文件的拖拽,ListBox中项的拖拽(可以多个之间进行),TreeView中项的多拽(如资源管理器的左侧树目录),ListView项的拖拽(如Windows中文件的拖放操作)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧