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

Drop both panels in a usercontrol, then drag the usercontrol.

You could also put both panels inside a panel

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:

Handlers:

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

Constructor:

public Form1()
{
    InitializeComponent();

    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

thanks! @tinstaafl

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.