【问题标题】:C# Drag and Drop between 2 Different ControlsC# 在 2 个不同的控件之间拖放
【发布时间】:2009-11-17 20:48:29
【问题描述】:

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

【问题讨论】:

  • 这是因为 sender 参数与发送事件的控件(即 TreeView)有关,而不是与谁开始拖放有关。

标签: c# controls drag-and-drop


【解决方案1】:

阿马尔,

正如 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
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2011-07-29
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多