Link to home
Start Free TrialLog in
Avatar of AndyAinscow
AndyAinscowFlag for Switzerland

asked on

Drag&Drop from a textbox - but still want to select text with the mouse

I'd like to perform a drag&drop from a textbox to another textbox.
If I trap the MouseDown event and start the DoDragDrop then I can perform the drag&drop.
eg.
        private void textBox_Adress_MouseDown(object sender, MouseEventArgs e)
        {
            textBox_Adress.DoDragDrop(textBox_Adress.Text, DragDropEffects.Copy);
        }

Open in new window

Unfortunately this then cripples the ability to select text within the textbox for modifying the contents of the textbox.
So.  How is the simplest way to allow the mouse to select text inside the textbox for editing AND allow the mouse to start a drag&drop of the complete contents of the textbox?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bob is right, you should use the MouseDown event. Look at the examples in the Control.DoDragDrop page of the online help, and you will get all the information that you need to perform the operation, from creating the icon or bitmap that will be used during the drag up to the moment the user drop what he was dragging.
Avatar of AndyAinscow

ASKER

Hi Bob, that is what I was thinking as one backup plan (I've got a couple of others that are lower down the pecking order).  Check the mouse move and only start the DoDragDrop as the mouse leaves the text box (unless there is a specific event firing on the mouse leaving the control) providing the left button was down.

I asked just in case there was a simple way like:
txtBox.MagicSetting = MagicSettings.On;
and then everything would still work.  I like simple ways to do things.
There is a MouseLeave event.  I'll look at using that.
The MouseLeave event didn't work as I hoped so I had to use the MouseMove in the end.   Turned out to be only a few lines of code