【问题标题】:Drag and Drop one control to another control in winform在winform中将一个控件拖放到另一个控件
【发布时间】:2012-07-23 17:46:06
【问题描述】:

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

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

    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's cursor

拖放不会发生..

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

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

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

我经历了thisthis

请帮忙...

【问题讨论】:

    标签: c# winforms drag-and-drop


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      检查 ListBox.AllowDrop 是否设置为 TRUE

      【讨论】:

      • 它在构造函数this.listBox1.AllowDrop = true;
      【解决方案3】:

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

      #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
      

      .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 2011-11-14
        相关资源
        最近更新 更多