locked
how to move multible controls in runtime RRS feed

  • Question

  • after searching in google i find one class that you can use it to give a control name and it will allow you to move this control and change his location in run time.

    my problem is how to make this class take multible controls to move it all in one time, like i have 100 buttons in form and i want in runtime move 5 in one time.

    this is the class:

    namespace Helper
    {
        class ControlMover
        {
            public enum Direction
            {
                Any,
                Horizontal,
                Vertical
            }
            public static void Init(Control control)
            {
                Init(control, Direction.Any);
            }
            public static void Init(Control control, Direction direction)
            {
                Init(control, control, direction);
            }
            public static void Init(Control control, Control container, Direction direction)
            {
                bool Dragging = false;
                Point DragStart = Point.Empty;
                control.MouseDown += delegate(object sender, MouseEventArgs e)
                {
                    Dragging = true;
                    DragStart = new Point(e.X, e.Y);
                    control.Capture = true;
                };
                control.MouseUp += delegate(object sender, MouseEventArgs e)
                {
                    Dragging = false;
                    control.Capture = false;
                };
                control.MouseMove += delegate(object sender, MouseEventArgs e)
                {
                    if (Dragging)
                    {
                        if (direction != Direction.Vertical)
                            container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                        if (direction != Direction.Horizontal)
                            container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
                    }
                };
            }
        }
    }

    and to use it i have to write this line:

    Helper.ControlMover.Init(this.button7);

    please any help in this ?

    Thanks for any help :) my blog is: http://www.waelk.com

    Tuesday, October 30, 2012 9:14 AM

Answers

  • Let's see if I understood: you want to be able to drag one button with the mouse and the other buttons you choose are dragged at the same time?

    Change the Init method to take a collection or array of controls instead of the 'container' control. In the MouseMove handler, apply the same movement to all those controls.

    Call the Init method for each button, passing all the controls as second parameter:

    Control[] controlsToMove = new Controls[] { button1, button2, button3 };
    foreach(Control control in controlsToMove)
    {
        Helper.ControlMover.Init(control, controlsToMove);
    }

    • Marked as answer by Bob Shen Thursday, November 8, 2012 8:28 AM
    Tuesday, October 30, 2012 12:34 PM