开发者

DoDragDrop from standard Label not working

I can't figure out why attempting to drag text from a standard Label to Notepad (or any other control accepting text) doesn't work. I've looked at documentation and examples and I'm not seeing the problem. The cursor remains a circle w开发者_开发百科ith a line through it and if I register a FeedBack callback the event is always NONE. Creating a standard Windows Forms Application, dropping a Label control and registering MouseDown & MouseMove events I have this code where I call label1.DoDragDrop (label1, DragDropEffects.All | DragDropEffects.Link). Any help would be appreciated.

Here is my form code:

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;
                }
            }
        }

    }
}


First, change

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

to

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

Second, you must prepare your drop target. Lets assume, it is textbox. Here is exmple extension method which will allow to cofigure any textbox by calling 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));
                }
            };
    }
}


Standard edit controls (textboxes) do not support drag&drop and will not accept any dropped text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜