views:

495

answers:

1

We have a ListView control which triggers the DoDragDrop method. We have another control which is a TreeView control which has a DragDrop method. The problem is that the DragDrop method's sender parameter is not the ListView, despite the fact that the ListView initiated the DoDragDrop method. Instead, the sender is the TreeView itself. Any ideas why the sender is incorrect?

+1  A: 

Amar,

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

An 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's 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
        }

    }
}
Ciwee