C # Drag and Drop between two different controls

We have a ListView control that runs the DoDragDrop method. We have another control that is a TreeView control that has a DragDrop method. The problem is that the sender parameter of the DragDrop method is not a ListView, even though the ListView initiated the DoDragDrop method. Instead, the sender is TreeView itself. Any ideas why the sender is wrong?

+3
source share
1 answer

Amar

as tyranid stated, the "sender" is the control that triggered the event. This control is never the control that started the drag and drop, but the control that accepted the drag.

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    /// <summary>
    /// There button 1 and button 2... button 1 is meant to start the dragging. Button 2 is meant to accept it
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// This is when button 1 starts the drag
        /// </summary>
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            this.DoDragDrop(this, DragDropEffects.Copy);
        }

        /// <summary>
        /// This is when button 2 accepts the drag
        /// </summary>
        private void button2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }


        /// <summary>
        /// This is when the drop happens
        /// </summary>
        private void button2_DragDrop(object sender, DragEventArgs e)
        {
            // sender is always button2
        }

    }
}
+1
source

Source: https://habr.com/ru/post/1723157/


All Articles