Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drag and drop winform controls

Tags:

c#

winforms

I want to drag and drop a control (label for example) in a winform application. I saw some examples on dragging and dropping text, but this is not what I want. I want to enable the user to move a control around. Can anyone direct me to some resources or examples? Thanks.

like image 589
FadelMS Avatar asked Sep 02 '11 07:09

FadelMS


2 Answers

you should look at examples on how to make draggable controls.

There are some answers here in SO as well.

See this Move controls when Drag and drop on panel in C#

this is a complete example on how to host the Form Designer:

Tailor Your Application by Building a Custom Forms Designer with .NET

I did something similar in Delphi long time ago, will search the source code, convert it into .NET C# and make a wiki page on that matter, as it is becoming such popular question recently :)

like image 155
Davide Piras Avatar answered Oct 19 '22 01:10

Davide Piras


As far as i understand, where you wish to drop a control is called a container, infact any control can act as a container. So first that container, you need to enable the drop property as well as the drag property of the controls which you need to drag.

Then write events (Candrag, candrop, controladded, etc.) for each control where in which, some logic to hold the objects and display them as you may want.

Say, ill take an example where in which, you wish to drag imagetext from combombox into a picturebox and then make the picturebox analyze the text and fine related file name in a directory and load that image into its if its present.

So here, when you start dragging the text from combombox, you have to write some logic in event candrag. Then once you drop, you have to write logic to understand what kinda object was added and get the text related to it (kinda deciphering) in the control where you drop other control.

Sorry, i have no code to give you now, but i hope you got the idea how its done. May be this article can help you? http://vicky4147.wordpress.com/2007/02/04/a-simple-drag-drop-in-winforms/

like image 41
Zenwalker Avatar answered Oct 19 '22 03:10

Zenwalker