【问题标题】:DoDragDrop from standard Label not working标准标签中的 DoDragDrop 不起作用
【发布时间】:2010-09-26 23:35:17
【问题描述】:

我不明白为什么尝试将文本从标准标签拖动到记事本(或任何其他接受文本的控件)不起作用。我查看了文档和示例,但没有发现问题。光标仍然是一个圆圈,有一条线穿过它,如果我注册一个反馈回调,事件总是无。创建一个标准的 Windows 窗体应用程序,删除一个标签控件并注册 MouseDown 和 MouseMove 事件我有这段代码,我调用 label1.DoDragDrop (label1, DragDropEffects.All | DragDropEffects.Link)。任何帮助将不胜感激。

这是我的表单代码:

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 DragDropLabel
{
    public partial class Form1 : Form
    {
        Point m_ClickLocation;
        bool _bDragging = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLabelMouseDown(object sender, MouseEventArgs e)
        {
            m_ClickLocation = e.Location;
            _bDragging = true;
        }

        private void OnLabelMouseMove(object sender, MouseEventArgs e)
        {
            if (_bDragging)
            {
                Point pt = e.Location;
                Size dragSize = SystemInformation.DragSize;
                if (Math.Abs(pt.X - m_ClickLocation.X) > dragSize.Width / 2 ||
                    Math.Abs(pt.Y - m_ClickLocation.Y) > dragSize.Height / 2)
                {
                    DragDropEffects rc = label1.DoDragDrop(label1, DragDropEffects.All | DragDropEffects.Link);
                    _bDragging = false;
                }
            }
        }

    }
}

【问题讨论】:

    标签: c# drag-and-drop label


    【解决方案1】:

    标准编辑控件(文本框)不支持拖放并且不接受任何拖放的文本。

    【讨论】:

    • 我认为任何从 Control 派生的控件都支持拖放。标签被用作拖放源而不是目标。
    • @AlanKley:我意识到这一点。但是,标准文本框不接受拖放,因此您不能拖放它们。如果您拖动到写字板或其他丰富的编辑控件,它应该可以正常工作。
    • 感谢 SLaks,如果我按照 Max 的建议进行更改以使用 lable1.text 而不是 label1,那么您是对的。
    【解决方案2】:

    首先,改变

    DragDropEffects rc = label1.DoDragDrop(label1, DragDropEffects.All | DragDropEffects.Link);
    

    label1.DoDragDrop(label1.Text, DragDropEffects.Copy);
    

    其次,您必须准备好您的投放目标。让我们假设,它是文本框。这是一个示例扩展方法,它允许通过调用 MyTextBox.EnableTextDrop() 来配置任何文本框:

    static class TextBoxExtensions
    {
        public static void EnableTextDrop(this TextBox textBox)
        {
            if(textBox == null) throw new ArgumentNullException("textBox");
    
            // first, allow drop events to occur
            textBox.AllowDrop = true;
            // handle DragOver to provide visual feedback
            textBox.DragOver += (sender, e) =>
                {
                    if(((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) &&
                        e.Data.GetDataPresent(typeof(string)))
                    {
                        e.Effect = DragDropEffects.Copy;
                    }
                };
            // handle DragDrop to set text
            textBox.DragDrop += (sender, e) =>
                {
                    if(((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) &&
                        e.Data.GetDataPresent(typeof(string)))
                    {
                        ((TextBox)sender).Text = (string)e.Data.GetData(typeof(string));
                    }
                };
        }
    }
    

    【讨论】:

    • 谢谢马克斯。我没有实现放置目标。我想进入其他应用程序编辑控件、记事本、extc。我错误地认为记事本是一个有效的放置目标,但实际上它不是。您正确地指出我需要传入 label1.Text!
    猜你喜欢
    • 2023-04-02
    • 2017-01-13
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2017-01-01
    • 2012-04-28
    • 2016-05-14
    相关资源
    最近更新 更多