Good day! I want to drag 2 panels at the same time during run time, I have 2 panels inside my winform and I made the 2nd panel as the header of the 1st panel.. my problem is when I drag the 2nd panel, the first panel gets left behind.. I hope you can help me. TIA

2 Weeks
Discussion Span
Last Post by vin_1

Probably the most adaptable is to use the same handler for the Mousedown and MouseMove events for each panel and the controls in each panel:


private Point MousePointerLocation;
private void control_MouseDown(object sender, MouseEventArgs e)
    if(e.Button == MouseButtons.Left)
        MousePointerLocation = e.Location;

private void control_MouseMove(object sender, MouseEventArgs e)
    if (e.Button == MouseButtons.Left)
        panel2.Left = e.X + panel2.Left - MousePointerLocation.X;
        panel2.Top = e.Y + panel2.Top - MousePointerLocation.Y;
        panel3.Left = e.X + panel3.Left - MousePointerLocation.X;
        panel3.Top = e.Y + panel3.Top - MousePointerLocation.Y;


public Form1()

    foreach (Panel p in this.Controls.OfType<Panel>())
        p.MouseDown += control_MouseDown;
        p.MouseMove += control_MouseMove;
        foreach (Control c in p.Controls)
            c.MouseDown += control_MouseDown;
            c.MouseMove += control_MouseMove;

Because this uses the same handler, any panel or control that the mouse can click into will drag both panels together.

I'm wondering if instead of 2 panels, if static sized and bordered labels would work better for a header

Edited by tinstaafl

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.