2012-07-25 98 views
1

正在做非常非常简单的事情。将一个控件拖放到winform中的另一个控件中

我有一个列表框,其活动设置是这样的:

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事件永远不会启动。事实上,拖动从未发生。 每当我试图拖动标签(文本),不允许窗口光标出现,而不是'DragDropEffects.Copy的光标

拖放不会发生..

,当我修改列表框(和相关的代码)接受文件从其他任何窗口放在它上面,这是完美的。

so..am无法执行从保存在窗体上的控件拖动到保存在同一窗体上的另一个控件。

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

我通过this,并通过this

请帮忙去...

回答

3

您的代码不工作实际。 您只需在事件处理程序中设置正确的拖动效果即可。

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

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

不客气:) – 2012-07-25 10:19:07

1

检查ListBox.AllowDrop设置为TRUE或不

+1

它在构造函数'this.listBox1.AllowDrop = true;' – 2012-07-25 10:07:06

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 

相关问题