469,950 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Drag and Drop

Hello, Newsgroupians:

I've been looking on the Internet for some resources to create a simple
drag-and-drop application, but they all seem too complicated or not what I
need.

I have created a Form with a ToolStrip that has a couple of buttons at the
top. What I want to do is allow the user to drag one of the button's from
the toolstrip to the client area. Once the user let's go, I want to create a
button in the client area at that point. This mimics Visual Studio when
visually creating Forms. Does anyone have any short code or resources that
could allow me to do this?

Thank you.
Trecius
Nov 5 '07 #1
2 4625
On Nov 5, 11:19 am, Trecius <Trec...@discussions.microsoft.comwrote:
Hello, Newsgroupians:

I've been looking on the Internet for some resources to create a simple
drag-and-drop application, but they all seem too complicated or not what I
need.

I have created a Form with a ToolStrip that has a couple of buttons at the
top. What I want to do is allow the user to drag one of the button's from
the toolstrip to the client area. Once the user let's go, I want to create a
button in the client area at that point. This mimics Visual Studio when
visually creating Forms. Does anyone have any short code or resources that
could allow me to do this?

Thank you.

Trecius
Someone else might be able to come up with something more graceful but
I would just do it all manually.

OnMouseDown = set bMoving = true;
OnMouseMove = if (bMoving) { (copy control and set location = mouse.X
and mouse.Y) }
OnMouseUp = Create new control on form at Mouse.X and Mouse.Y set
bMoving = false;

Nov 5 '07 #2
Trecius wrote:
Hello, Newsgroupians:

I've been looking on the Internet for some resources to create a simple
drag-and-drop application, but they all seem too complicated or not what I
need.
Here are some simple explanations:

http://tinyurl.com/yeb6am
http://tinyurl.com/2k7zo2

>
I have created a Form with a ToolStrip that has a couple of buttons at the
top. What I want to do is allow the user to drag one of the button's from
the toolstrip to the client area. Once the user let's go, I want to create a
button in the client area at that point. This mimics Visual Studio when
visually creating Forms. Does anyone have any short code or resources that
could allow me to do this?
Here's some sample code:

using System.Windows.Forms;
using System.Drawing;
using System;

namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

newToolStripButton.MouseDown += newToolStripButton_MouseDown;
newToolStripButton.MouseUp += newToolStripButton_MouseUp;
newToolStripButton.MouseMove += newToolStripButton_MouseMove;
newToolStripButton.MouseLeave += newToolStripButton_MouseLeave;

AllowDrop = true;// just for demo purposes :-)
}
void newToolStripButton_MouseLeave(object sender, EventArgs e)
{
isDragging = false;
}
void newToolStripButton_MouseMove(object sender, MouseEventArgs e)
{
if(!isDragging)
return;
Size dragSize = SystemInformation.DragSize;
int dx = Math.Abs(dragPoint.X - e.X);
int dy = Math.Abs(dragPoint.Y - e.Y);

if(dx < dragSize.Width || dy < dragSize.Height)
return;
IDataObject data = new DataObject();
data.SetData(newToolStripButton);// D&D data
DragDropEffects res = DoDragDrop(data, DragDropEffects.All
| DragDropEffects.Link);
}
void newToolStripButton_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
void newToolStripButton_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
dragPoint = new Point(e.X, e.Y);
}
private bool isDragging = false;
private Point dragPoint = new Point();

protected override void OnDragOver(DragEventArgs drgevent)
{
if (drgevent.Data.GetDataPresent(typeof(ToolStripButt on)))
drgevent.Effect = DragDropEffects.Link;
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
if (!drgevent.Data.GetDataPresent(typeof(ToolStripBut ton)))
return;
MessageBox.Show(((ToolStripButton)
drgevent.Data.GetData(typeof (ToolStripButton))).Text);
}
}
}
HTH,
Andy

--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net
Nov 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by hemant_mishal | last post: by
reply views Thread by Lauren Quantrell | last post: by
2 posts views Thread by SamSpade | last post: by
3 posts views Thread by Ajay Krishnan Thampi | last post: by
6 posts views Thread by jojobar | last post: by
3 posts views Thread by VB Programmer | last post: by
reply views Thread by ViRi | last post: by
1 post views Thread by Darren | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.