简体   繁体   English

将一个控件拖放到Winform中的另一个控件

[英]Drag and Drop one control to another control in winform

am doing something very very simple. 我做的事情非常非常简单。

I have a listbox whose events are set like this : 我有一个列表框,其事件设置如下:

    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;
    }

now I have a label, code for which is as follows: 现在我有一个标签,其代码如下:

    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.

    }

but nothing happens. 但什么也没发生。 listbox DragEnter event never fires up. 列表框DragEnter事件从不触发。 in fact, drag never happens. 实际上,拖曳永远不会发生。 whenever i try to drag label (text), not allowed windows cursor appears, instead of ' DragDropEffects.Copy 's cursor 每当我尝试拖动标签(文本)时,出现不允许的Windows光标,而不是' DragDropEffects.Copy的光标

Drag and Drop doesn't take place.. 拖放不会发生。

when I modify the listbox (and the associated code) to accept files to be dropped on it from any other window, that works perfectly. 当我修改列表框(及其关联的代码)以接受要从任何其他窗口拖放到其上的文件时,效果很好。

so..am unable to perform drag from a control kept on the form to another control kept on the same form. so..am无法执行从窗体上保留的控件到同一窗体上另一个控件的拖动。

am I missing something? 我错过了什么吗? am running windows XP. 正在运行Windows XP。

I went through this and through this 我经历了这个 ,并通过这种

please help... 请帮忙...

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;
}

检查ListBox.AllowDrop是否设置为TRUE

The following is an example of what you need, with all the code (adding it here for whoever finds this post). 以下是所有代码的示例(将其添加到此处的人员)。

#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

.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM