views:

26

answers:

2

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

    }
}
+1  A: 

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

SLaks
I thought any control derived from Control supports Drag and drop. The Label is being used as drag and drop source not a target.
AlanKley
SLaks
Thanks SLaks, you're right about Wordpad vs Notepad, if I make the change suggested by Max to use lable1.text rather than label1.
AlanKley
+1  A: 

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));
                }
            };
    }
}
max
Thanks Max. I'm not implementing the drop target. I'm wanting to drop into other apps edit control, notepad, extc. I mistakenly thought that Notepad was a valid drop target when it appears it isn't. You correctly pointed out I needed to pass in label1.Text!
AlanKley