下面的代码示例演示如何使用 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); }
我现在一直在用一个第三方控件,别的公司开发的
具体什么名字不记得了,你在网上搜搜吧
不知道楼主的要求是什么,我没看明白,请说清楚一点
{
// 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);
}