I have two panels on a form.
One panel has some controls like buttons or images and second panel is empty. I want to drag a control from panel 1 and drop it to panel 2, but it should create a copy of control, and while dragging a rectangle should be shown of same size as control and when dropped in panel 2 the dragged shape should appear there at mouse position
Actually I want to create a simulator like thing. That has some tools in panel 1 and when someone drag and drop a tool on panel 2 it should appear there at mouse position.
Language doesn't matter may be C#
or VB.NET
Did you try something like this ?
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(c);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
VB.NET
Private Sub Form5_Load(sender As Object, e As EventArgs)
Me.panel1.AllowDrop = True
For Each c As Control In Me.panel1.Controls
c.MouseDown += New MouseEventHandler(AddressOf c_MouseDown)
Next
Me.panel1.DragOver += New DragEventHandler(AddressOf panel1_DragOver)
Me.panel1.DragDrop += New DragEventHandler(AddressOf panel1_DragDrop)
End Sub
Private Sub c_MouseDown(sender As Object, e As MouseEventArgs)
Dim c As Control = TryCast(sender, Control)
c.DoDragDrop(c, DragDropEffects.Move)
End Sub
Private Sub panel1_DragDrop(sender As Object, e As DragEventArgs)
Dim c As Control = TryCast(e.Data.GetData(e.Data.GetFormats()(0)), Control)
If c IsNot Nothing Then
c.Location = Me.panel1.PointToClient(New Point(e.X, e.Y))
Me.panel1.Controls.Add(c)
End If
End Sub
Private Sub panel1_DragOver(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.Move
End Sub
Source
Am changing a little of code of @Shim. Here is the updated code in which a copy of your control will be placed in another panel
Random rnd = new Random();
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
// Here, you get a copy of your drag drop button and dynamically new button is created
Button btn = new Button();
btn.Name = "Button" + rnd.Next();
btn.Size = c.Size;
if (c != null)
{
// Add the newly created button to you Panel
btn.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(btn);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With