Move button controls in a Windows Forms application inside a panel container

2

I have a form with three button controls, they are: btnSales, btnFunctionaries, btnConfig. And they are aligned inside a Container Panel. I need these buttons to be clicked and held by the mouse pointer so that the user can place them in the place that they prefer only by dragging them from one position to another, the space of displacement being allowed only within the area of the Panel container that the encompasses.

    
asked by anonymous 06.06.2017 / 04:10

1 answer

0
private void Form1_Load(object sender, EventArgs e)
{
    panel1.AllowDrop = true;
    panel1.DragEnter += Panel1_DragEnter;
    panel1.DragDrop += Panel1_DragDrop;            
    button2.MouseDown += Button2_MouseDown;
}


private void Button2_MouseDown(object sender, MouseEventArgs e)
{
    button2.DoDragDrop(button2, DragDropEffects.Move);
}

private void Panel1_DragDrop(object sender, DragEventArgs e)
{
    var btn = ((Button)e.Data.GetData(typeof(Button)));
    var point = panel1.PointToClient(new Point(e.X, e.Y));
    btn.Left = point.X;
    btn.Top = point.Y;
}

private void Panel1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}
    
06.06.2017 / 12:28