Note that there are some explanatory texts on larger screens.

plurals
  1. PODragDrop between controls
    primarykey
    data
    text
    <p>I have a problem with DragDrop.</p> <pre><code> private void Form0_Load(object sender, EventArgs e) { PictureBox panel1 = new PictureBox(); PictureBox panel2 = new PictureBox(); mainPanel.Dock = DockStyle.Fill; this.Controls.Add(mainPanel); panel1.Location = new Point(10, 10); panel1.Size = new System.Drawing.Size(500, 300); panel1.BorderStyle = BorderStyle.FixedSingle; Button b2 = new Button(); b2.Location = new Point(10, 10); panel2.Controls.Add(b2); panel2.Location = new Point(10, 10); panel2.Size = new System.Drawing.Size(200, 100); panel2.BorderStyle = BorderStyle.FixedSingle; foreach (Control c in panel1.Controls) { c.MouseDown += new MouseEventHandler(control_MouseDown); c.MouseMove += new MouseEventHandler(control_MouseMove); c.MouseUp += new MouseEventHandler(control_MouseUp); c.AllowDrop = true; } panel1.AllowDrop = true; panel1.DragEnter += new DragEventHandler(container_DragEnter); panel1.DragDrop += new DragEventHandler(container_DragDrop); panel1.DragOver += new DragEventHandler(container_DragOver); foreach (Control c in panel2.Controls) { c.MouseDown += new MouseEventHandler(control_MouseDown); c.MouseMove += new MouseEventHandler(control_MouseMove); c.MouseUp += new MouseEventHandler(control_MouseUp); c.AllowDrop = true; } panel2.AllowDrop = true; panel2.DragEnter += new DragEventHandler(container_DragEnter); panel2.DragDrop += new DragEventHandler(container_DragDrop); panel2.DragOver += new DragEventHandler(container_DragOver); mainPanel.Controls.Add(panel1); mainPanel.Controls.Add(panel2); mainPanel.Controls.Add(pb); } private void control_MouseDown(object sender, MouseEventArgs e) { Control c = sender as Control; isDragging = true; clickOffsetX = e.X; clickOffsetY = e.Y; } private void control_MouseMove(object sender, MouseEventArgs e) { Control c = sender as Control; if (isDragging == true) { c.Left = e.X + c.Left - clickOffsetX; c.Top = e.Y + c.Top - clickOffsetY; if (c.Location.X + clickOffsetX &gt; c.Parent.Width || c.Location.Y + clickOffsetY &gt; c.Parent.Height || c.Location.X + clickOffsetX &lt; 0 || c.Location.Y + clickOffsetY &lt; 0) c.DoDragDrop(c, DragDropEffects.Move); } } private void control_MouseUp(object sender, MouseEventArgs e) { isDragging = false; } void container_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void container_DragEnter(object sender, DragEventArgs e) { //e.Effect = DragDropEffects.Move; //if (e.Data.GetDataPresent(typeof(Bitmap))) //{ // e.Effect = DragDropEffects.Copy; //} //else //{ // e.Effect = DragDropEffects.None; //} } private void container_DragDrop(object sender, DragEventArgs e) { Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control; PictureBox p = sender as PictureBox; mycontrol = c; isDragging = false; if (c != null) { c.Location = p.PointToClient(new Point(e.X, e.Y)); p.Controls.Add(c); } } </code></pre> <p>This is a working example. But I can't do drop Controls from parent to child control. What is a magic? How to drop control to another control (from panel1 to panel2 in my example).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload