Drag and Drop

G

Guest

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
 
C

cfps.Christian

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

Andy Mueller

Trecius said:
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(ToolStripButton)))
drgevent.Effect = DragDropEffects.Link;
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
if (!drgevent.Data.GetDataPresent(typeof(ToolStripButton)))
return;
MessageBox.Show(((ToolStripButton)
drgevent.Data.GetData(typeof (ToolStripButton))).Text);
}
}
}


HTH,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top