1

我正在做一些非常非常简单的事情。

我有一个列表框,其事件设置如下:

    public Form1()
    {
        InitializeComponent();
        this.listBox1.AllowDrop = true;
        this.listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter);
        this.listBox1.DragDrop += new DragEventHandler(listBox1_DragDrop);
    }

    void listBox1_DragDrop(object sender, DragEventArgs e)
    {
       //code to add labelText to Items of ListBox
    }

    void listBox1_DragEnter(object sender, DragEventArgs e)
    {
        //set DragDropEffects;
    }

现在我有一个标签,代码如下:

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
        //this.label1.DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
        //used one of them at a time.

    }

但什么也没发生。列表框 DragEnter 事件永远不会触发。事实上,阻力永远不会发生。每当我尝试拖动标签(文本)时,出现不允许的 windows 光标,而不是DragDropEffects.Copy' 的光标

拖放不会发生..

当我修改列表框(和相关代码)以接受要从任何其他窗口拖放到其上的文件时,效果很好。

所以..我无法从保持在表单上的控件拖动到保持在同一表单上的另一个控件。

我错过了什么吗?我正在运行 Windows XP。

我经历了这个,经历了这个

请帮忙...

4

3 回答 3

3

Your code does work actually. You just have to set the right drag effects in your event handlers.

void listBox1_DragDrop(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}

void listBox1_DragEnter(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}
于 2012-07-25T10:11:49.563 回答
2

检查 ListBox.AllowDrop 是否设置为 TRUE

于 2012-07-25T10:05:59.240 回答
0

以下是您需要的示例以及所有代码(将其添加到此处以供找到此帖子的人使用)。

#region Initial Values
//Constructor:
public Form1() {
   InitializeComponent();
}

private void Form1_Load( object sender, EventArgs e ) {
   InitialValues();
}

private void InitialValues() {
   PrepareDragAndDrop();
}
#endregion Initial Values

#region Drag & Drop

private void PrepareDragAndDrop() {
   //For the object that receives the other dragged element:
   TheSamplListBox.AllowDrop = true;
   TheSamplListBox.DragEnter += TheSamplListBox_DragEnter;
   TheSamplListBox.DragLeave += TheSamplListBox_DragLeave;
   TheSamplListBox.DragDrop  += TheSamplListBox_DragDrop;

   //For the object that will be dragged:
   TheSampleLabel.MouseDown += ( sender, args ) => DoDragDrop( TheSampleLabel.Text, DragDropEffects.Copy );
}

private void TheSamplListBox_DragEnter( object theReceiver, DragEventArgs theEventData ) {
   theEventData.Effect = DragDropEffects.Copy;

   //Only the code above is strictly for the Drag & Drop. The following is for user feedback:
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   theReceiverListBox.BackColor = Color.LightSteelBlue;
}

private void TheSamplListBox_DragLeave( object theReceiver, EventArgs theEventData ) {
   //No code here for the Drag & Drop. The following is for user feedback:
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   theReceiverListBox.BackColor = Color.White;
}

private void TheSamplListBox_DragDrop( object theReceiver, DragEventArgs theEventData ) {
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   //Get the data being dropped. In this case, a string:
   var theStringBeingDropped = theEventData.Data.GetData( "System.String" );

   //Add the string to the ListBox:
   theReceiverListBox.Items.Add( theStringBeingDropped );

   //Only the code above is strictly for the Drag & Drop. The following is for user feedback:
   theReceiverListBox.BackColor = Color.White;
}
#endregion Drag & Drop

.

于 2016-08-05T00:22:03.700 回答