关于c#:将一个控件拖放到winform中的另一个控件

Drag and Drop one control to another control in winform

我做的事情非常简单。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    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;
    }

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

1
2
3
4
5
6
7
    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.

    }

但什么也没发生。列表框DragCenter事件从不激发。事实上,拖动从未发生过。每当我试图拖动标签(文本)时,不允许出现Windows光标,而不是"DragDropEffects.Copy的光标

不会发生拖放。

当我修改列表框(以及相关的代码)以接受从任何其他窗口放到列表框上的文件时,这是非常有效的。

因此..无法执行从窗体上保留的控件到同一窗体上保留的其他控件的拖动。

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

我经历过这个和这个

请帮忙…


你的代码确实有效。只需在事件处理程序中设置正确的拖动效果。

1
2
3
4
5
6
7
8
9
void listBox1_DragDrop(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}

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


检查listbox.allowDrop是否设置为true


下面是一个您需要的示例,其中包含所有代码(为找到此文章的人添加此代码)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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

.