C# 在两个不同控件之间拖放

标签 c# controls drag-and-drop

我们有一个触发 DoDragDrop 方法的 ListView 控件。我们有另一个控件,它是一个 TreeView 控件,它有一个 DragDrop 方法。问题在于 DragDrop 方法的 sender 参数不是 ListView,尽管 ListView 启动了 DoDragDrop 方法。相反,发送者是 TreeView 本身。知道为什么发件人不正确吗?

最佳答案

阿马尔,

正如 tyranid 所说,“发送者”是触发事件的控件。此控件绝不是开始拖动的控件,而是接受拖动的控件。

一个例子:

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
        }

    }
}

关于C# 在两个不同控件之间拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1751663/

相关文章:

c# - 如何根据复选框状态更改列表框选择,反之亦然?

ios - (iOS) 拖放、对齐网格、勾选合适区域

c# - 在 C# 中,我如何停止执行来自其绑定(bind)委托(delegate)之一的事件?

c# - 为什么窗口的卸载事件不会在 WPF 中触发?

asp.net - 找到多个具有相同 ID 'add0' 的控件。 FindControl 要求控件具有唯一 ID

c# - 防止多次重复选择同步控件?

macos - NSView拖动操作错误

javascript - 拖放图像上传不适用于某些浏览器

c# - 在 TabContainer 中定位选项卡

c# - 最终用户将值添加到下拉列表中?