Drag and Change Position of a Groupbox at Runtime

Asked

Viewed 364 times

6

I have a groupbox that I wish the user could drag and change the position at runtime, someone imagines some way to do this?

  • 1

    Windows Forms or WPF?

  • In Windows Forms

  • change the position of the groupbox or the items inside it?

  • I need a behavior as if it were a DOC, I have a groupbox with some buttons inside it, I wish the User could be able to move these buttons as he likes to leave where he wants.

3 answers

9


There is nothing ready for this in Windows Forms, but it is possible to work with the events of mouse and locating controls to have this effect.

I have here a class with extensions that does just that. I remember that I needed it for a project and I found it in some blog, I will look for the author to give due credit. By the way, she may have been modified.

The only thing that is needed to make a control become "draggable" is controle.Draggale(true) and controle.Draggale(false) to deactivate.

See in operation.

GIF de um groupbox sendo arrastado pela tela

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

6

Looking fast here, would start by 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 vs graphical interface, so you have to add via code, constructor or form load event.

and

It’s just the beginning of the implementation... so it’s not cool yet, the control is blinking when dragging and by the location of the mouse, without considering the initial location, but it’s a start. I hope it helps.

0

Taking advantage of the Friend Code "Rovann Linhalis", I adjusted my needs: I created a button named btn_drag and assigns 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);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.