Drag and Change the Position of a GroupBox at Runtime

6

I have a groupbox that I want the user to be able to drag and change the position at runtime, does anyone figure out any way to do that?

    
asked by anonymous 17.10.2017 / 19:34

3 answers

9

Nothing is ready for this in Windows Forms, but it is possible to work with mouse events and location of controls to have this effect.

I have here a class with extensions that does just that. I remember I needed a project and I found it on some blog, I'm going to look for the author to give the credits. By the way, it may have been modified.

The only thing that is required to make a control "drag" is controle.Draggale(true) and controle.Draggale(false) to disable.

See it working.

public static class ControlExtension
{
    private static readonly Dictionary<Control, bool> Draggables 
                                             = new Dictionary<Control, bool>();
    private static Size _mouseOffset;

    public static void Draggable(this Control control, bool enable)
    {
        if (enable)
        {
            if (Draggables.ContainsKey(control))
            {
                return;
            }
            Draggables.Add(control, false);

            control.MouseDown += control_MouseDown;
            control.MouseUp += control_MouseUp;
            control.MouseMove += control_MouseMove;
        }
        else
        {
            if (!Draggables.ContainsKey(control))
            {  
                return;
            }

            control.MouseDown -= control_MouseDown;
            control.MouseUp -= control_MouseUp;
            control.MouseMove -= control_MouseMove;
            Draggables.Remove(control);
        }
    }

    private static void control_MouseDown(object sender, MouseEventArgs e)
    {
        _mouseOffset = new Size(e.Location);
        Draggables[(Control)sender] = true;
    }

    private static void control_MouseUp(object sender, MouseEventArgs e)
    {
        Draggables[(Control)sender] = false;
    }

    private static void control_MouseMove(object sender, MouseEventArgs e)
    {
        if (!Draggables[(Control) sender])
            return;

        var newLocationOffset = e.Location - _mouseOffset;
        ((Control)sender).Left += newLocationOffset.X;
        ((Control)sender).Top += newLocationOffset.Y;
    }
}
    
17.10.2017 / 20:09
6

Looking fast here, it would start with the event MouseMove :

 public Form1()
 {
    InitializeComponent();
    groupBox1.MouseMove += groupBox1_MouseMove;
 }

...

    void groupBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            groupBox1.Location = e.Location;
        }

    }
  

The event is not displayed in the graphical interface of vs, so you have to add via code, in the constructor or load event of the form.

and

  

It's just the beginning of the implementation ... this way it's still not cool, the control blinks when you drag and the location of the mouse, regardless of the initial location, but it's a start. Hope it helps.

    
17.10.2017 / 19:59
0

Taking advantage of the Friend Code "Rovann Linhalis", I adjusted my Needs: I created a button with name btn_arrastar and assigned the MouseMove event.

void btn_arrastarMouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        btn_arrastar.Location = new Point(Control.MousePosition.X - btn_arrastar.Size.Height/2,Control.MousePosition.Y - btn_arrastar.Size.Width/2);
    }
}
    
19.07.2018 / 23:26