Click here to Skip to main content
Licence 
First Posted 29 Sep 2003
Views 133,679
Bookmarked 85 times

Simple Runtime Control Sizing and Dragging Class

By | 29 Sep 2003 | Article
Sample and brief description of simple class that enables sizing and dragging of controls on a form

Figure - Form showing selected Label control, following selection

Introduction

The PickBox class provides sizing handles that allow the positioning and sizing on simple controls on a containing form. This C# sample was adapted from an earlier version of the class written in VB (version 6). The sample was prepared using Borland's C# Builder IDE and exported to a VS project.

Using the code

The PickBox class exposes a "WireControl" method that attaches events to a passed control, implementing “pickbox” behavior. Clicking on a “wired” control displays eight sizing handles around the perimeter of the control and enables sizing and dragging of the control via mouse event handlers provided by the class instance (see commented code for details). The following snippet illustrates the use of the PickBox class and this function from within the Sample Form:

//(Excerpt from Winform.cs)
//
// Create an instance of the PickBox class
//
private PickBox pb = new PickBox();
public WinForm() // Sample Form's constuctor
{
    InitializeComponent();
    //
    // Provide a Click event handler for each control
    // that attaches a pick box to the control when clicked
    //
    foreach (Control c in this.Controls) {
        pb.WireControl(c);
    }
}

The "WireControl" method attaches a Click event handler to each passed control. When called the event handler then attaches the "pickbox", made up of eight Label controls that act as sizing handles, to the clicked control. In addition, mouse event handlers are attached to the control allowing for dragging of the control on its parent form.

//(Excerpt from PickBox.cs)
private void SelectControl(object sender, EventArgs e) {
    if (m_control is Control) {
        m_control.Cursor = oldCursor;
        // Remove event any event handlers appended to last control
        // by this class
        m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
        m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
        m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
        m_control.Click -= new EventHandler(this.SelectControl);
        m_control = null;
    }
    m_control = (Control)sender;
    //Add event handlers for moving the selected control around
    m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
    m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
    m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp);
    //Add sizing handles to Control's container (Form or PictureBox)
    for (int i = 0; i<8; i++) {
        m_control.Parent.Controls.Add(lbl[i]);
        lbl[i].BringToFront();
    }
    //Position sizing handles around Control
    MoveHandles();
    //Display sizing handles
    ShowHandles();
    oldCursor = m_control.Cursor;
    m_control.Cursor = Cursors.SizeAll;
}

The sizing handles are Labels that are created, initialized and stored in an array of Label controls when the instance of the PickBox class is constructed. MouseDown, MouseMove, and MouseUp events service the array of Labels during control sizing operations.

Points of Interest

The class sample works well for simple applications, but may exhibit some interaction within applications employing more complicated, time-critical event handling. In it’s current form it provides for the selection of only one control at a time.

The PickBox sample is a simpler, and probably less versatile C# example of the functionality presented in the C++ sample “A Sizing/Moving widget” by Andrew JM Hall.

History

  • This is the initial submission of the sample.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jim Korovessis

Program Manager
General Dynamics Canada
Canada Canada

Member

Manager, Customer Training in Ottawa, ON, Canada
www.gdcanada.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to create multiple selection on panel. Pinmembertinku321:04 13 May '12  
GeneralThank Very Much Pinmembersanaz13x23:37 5 Jan '12  
SuggestionGreat article PinmemberSudarshan.chandan@gs.com22:34 13 Oct '11  
QuestionGreat patent - Any license? PinmemberMember 769042613:50 9 Oct '11  
AnswerRe: Great patent - Any license? PinmemberJim Korovessis1:39 11 Oct '11  
GeneralMy vote of 5 PinmemberBharat Mallapur1:01 14 Apr '11  
AnswerUpdated with multiple selection Pinmemberecklerpa9:17 21 Apr '09  
GeneralPickBox and Pictureboxes PinmemberJuwi_uk5:04 4 Mar '09  
GeneralMultiple controls drag n drop. [modified] PinmemberVB 8.01:45 20 Feb '09  
GeneralGreate code Pinmemberammar7922:04 26 Jan '09  
GeneralAdaptation for multiple selection (more than one selected control). [modified] PinmemberDiego Osorio16:43 26 Dec '08  
Hi all. Thank you Jim for your great work Wink | ;)
 
I'm developing a designer that use your code. But, I had a little issue: I need to select more than one control, to allow resizing and moving in group.
 
I send your code with my own implementation for that multiple selection (C#, VS 2005). I hope this help to someone (sorry for the long message):
 
    /// [summary]
    /// This class implements sizing and moving functions for
    ///	runtime editing of graphic controls
    /// [/summary]
    public class PickBox
    {
        //////////////////////////////////////////////////////////////////
        // PRIVATE CONSTANTS AND VARIABLES
        //////////////////////////////////////////////////////////////////

        private const int BOX_SIZE = 8;
        private Color BOX_COLOR = Color.White;
        // private ContainerControl m_container;
        private Label[] lbl = new Label[8];
        private int startl;
        private int startt;
        private int startw;
        private int starth;
        private int startx;
        private int starty;
        private bool dragging;
        private bool moved;
        private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,
			Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,
			Cursors.SizeNESW, Cursors.SizeWE};
 
        private const int MIN_SIZE = 20;
 
        //
        // Constructor creates 8 sizing handles & wires mouse events
        // to each that implement sizing functions
        //
        public PickBox()
        {
            for (int i = 0; i < 8; i++)
            {
                lbl[i] = new Label();
                lbl[i].TabIndex = i;
                lbl[i].FlatStyle = 0;
                lbl[i].BorderStyle = BorderStyle.FixedSingle;
                lbl[i].BackColor = BOX_COLOR;
                lbl[i].Cursor = arrArrow[i];
                lbl[i].Text = "";
                lbl[i].BringToFront();
                lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown);
                lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove);
                lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp);
            }
        }
 
        //////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //////////////////////////////////////////////////////////////////

        //
        // Wires a Click event handler to the passed Control
        // that attaches a pick box to the control when it is clicked
        //
        public void WireControl(Control ctl)
        {
            ctl.Click += new EventHandler(this.SelectControl);
        }
 
        private Hashtable SelectedControlCursors = new Hashtable();
 
        private List[Control] _SelectedControls = new List[Control]();
        /// [summary]
        /// Get the current selected controls.
        /// [/summary]
        public List[Control] SelectedControls
        {
            get
            {
                return _SelectedControls;
            }
        }
 
        /// [summary]
        /// Deselect all controls.
        /// [/summary]
        public void ClearSelection()
        {
            while (SelectedControls.Count > 0)
            {
                RemoveControl(SelectedControls[0]);
            }
            HideHandles();
        }
 
        /// [summary]
        /// Return true if the current selection has no controls.
        /// [/summary]
        private bool IsEmptySelection
        {
            get
            {
                return SelectedControls.Count == 0;
            }
        }
 
        /////////////////////////////////////////////////////////////////
        // PRIVATE METHODS
        /////////////////////////////////////////////////////////////////

        /// [summary]
        /// Add or remove a control from the current selection.
        /// [/summary]
        private void AddOrRemoveControl(Control c)
        {
            if (SelectedControls.Contains(c))
                RemoveControl(c);
            else
                AddControl(c);
        }
 
        private void AddControl(Control c)
        {
            //Add event handlers for moving the current selected controls around.
            c.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
            c.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
            c.MouseUp += new MouseEventHandler(this.ctl_MouseUp);
 
            // Remove click handler
            c.Click -= new EventHandler(this.SelectControl);
 
            // Register the current cursor of the added control for restoring it later.
            SelectedControlCursors.Add(c, c.Cursor);
 
            // Change cursor of the control to the "moving" cursor.
            c.Cursor = Cursors.SizeAll;
 
            // Add to current selection
            SelectedControls.Add(c);
        }
 
        private void RemoveControl(Control c)
        {
            // Restore cursor of the control.
            c.Cursor = (Cursor)SelectedControlCursors[c];
            SelectedControlCursors.Remove(c);
 
            //Remove event any pre-existing event handlers appended by this class
            c.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
            c.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
            c.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
 
            // Restore click handler
            c.Click += new EventHandler(this.SelectControl);
 
            // Remove from current selection
            SelectedControls.Remove(c);
        }
 
        /// [summary]
        /// Get the current selection boundary, on an empty rectangle if the current
        /// selection is also empty.
        /// [/summary]
        private Rectangle GetSelectionBounds()
        {
            if (SelectedControls.Count == 0)
            {
                return Rectangle.Empty;
            }
 
            int l = int.MaxValue;
            int r = int.MinValue;
            int t = int.MaxValue;
            int b = int.MinValue;
            foreach (Control c in SelectedControls)
            {
                l = Math.Min(l, c.Left);
                r = Math.Max(r, c.Right);
                t = Math.Min(t, c.Top);
                b = Math.Max(b, c.Bottom);
            }
            return new Rectangle(l, t, r - l, b - t);
        }
 
        //
        // Attaches a pick box to the sender Control
        //
        private void SelectControl(object sender, EventArgs e)
        {
            Control c = (Control)sender;
            
            AddOrRemoveControl(c);
 
            if (IsEmptySelection)
            {
                HideHandles();
                return;
            }
 
            //Add sizing handles to Control's container (Form or PictureBox)
            Control container = SelectedControls[0].Parent;
            for (int i = 0; i < 8; i++)
            {
                if (lbl[i].Parent == null) container.Controls.Add(lbl[i]);
                lbl[i].BringToFront();
            }
 
            //Position sizing handles around Control
            MoveHandles();
 
            //Display sizing handles
            ShowHandles();
        }
 
        private void ShowHandles()
        {
            if (!IsEmptySelection)
            {
                for (int i = 0; i < 8; i++)
                {
                    lbl[i].Visible = true;
                }
            }
        }
 
        private void HideHandles()
        {
            for (int i = 0; i < 8; i++)
            {
                lbl[i].Visible = false;
            }
        }
 
        private void MoveHandles()
        {
            Rectangle bounds = GetSelectionBounds();
 
            int sX = bounds.Left - BOX_SIZE;
            int sY = bounds.Top - BOX_SIZE;
            int sW = bounds.Width + BOX_SIZE;
            int sH = bounds.Height + BOX_SIZE;
            int hB = BOX_SIZE / 2;
            int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,
			sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};
            int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,
			sY + sH-hB, sY + sH-hB, sY + sH / 2};
            for (int i = 0; i < 8; i++)
                lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
        }
 
        /////////////////////////////////////////////////////////////////
        // MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL
        /////////////////////////////////////////////////////////////////

        //
        // Store control position and size when mouse button pushed over
        // any sizing handle
        //
        private void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            Rectangle bounds = GetSelectionBounds();
            startl = bounds.Left;
            startt = bounds.Top;
            startw = bounds.Width;
            starth = bounds.Height;
            HideHandles();
        }
 
        //
        // Size the picked control in accordance with sizing handle being dragged
        //	0   1   2
        //  7       3
        //  6   5   4
        //
        private void lbl_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle bounds = GetSelectionBounds();
            int l = bounds.Left;
            int w = bounds.Width;
            int t = bounds.Top;
            int h = bounds.Height;
            if (dragging)
            {
                switch (((Label)sender).TabIndex)
                {
                    case 0: // Dragging top-left sizing box
                        l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
                        t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
                        w = startl + startw - bounds.Left;
                        h = startt + starth - bounds.Top;
                        break;
                    case 1: // Dragging top-center sizing box
                        t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
                        h = startt + starth - bounds.Top;
                        break;
                    case 2: // Dragging top-right sizing box
                        w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
                        t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
                        h = startt + starth - bounds.Top;
                        break;
                    case 3: // Dragging right-middle sizing box
                        w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
                        break;
                    case 4: // Dragging right-bottom sizing box
                        w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
                        h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
                        break;
                    case 5: // Dragging center-bottom sizing box
                        h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
                        break;
                    case 6: // Dragging left-bottom sizing box
                        l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
                        w = startl + startw - bounds.Left;
                        h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
                        break;
                    case 7: // Dragging left-middle sizing box
                        l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
                        w = startl + startw - bounds.Left;
                        break;
                }
                l = (l < 0) ? 0 : l;
                t = (t < 0) ? 0 : t;
 
                ChangeSelectionBounds(l, t, w, h);
            }
        }
 
        /// [summary]
        /// Change the current selection bounds (therefore, changing positions and sizes of each control
        /// in the selection).
        /// [/summary]
        private void ChangeSelectionBounds(int l, int t, int w, int h)
        {
            Rectangle oldB = GetSelectionBounds();
 
            int deltaX = l - oldB.Left;
            int deltaY = t - oldB.Top;
            int deltaW = w - oldB.Width;
            int deltaH = h - oldB.Height;
 
            // Change location and size of each select control.
            foreach(Control c in SelectedControls)
            {
                c.SetBounds(c.Left + deltaX, c.Top + deltaY, c.Width + deltaW, c.Height + deltaH);
            }
        }
 
        //
        // Display sizing handles around picked control once sizing has completed
        //
        private void lbl_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
            MoveHandles();
            ShowHandles();
        }
 
        /////////////////////////////////////////////////////////////////
        // MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM
        /////////////////////////////////////////////////////////////////

        //
        // Get mouse pointer starting position on mouse down and hide sizing handles
        //
        private void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            moved = false;
            startx = e.X;
            starty = e.Y;
            HideHandles();
        }
 
        //
        // Reposition the dragged control
        // (Updated for global reposition -on each selected control)
        //
        private void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                int deltaX = e.X - startx;
                int deltaY = e.Y - starty;
 
                Rectangle bounds = GetSelectionBounds();
                Control parent = SelectedControls[0].Parent;
 
                if (bounds.Left + deltaX < 0)
                    deltaX = bounds.Left * -1;
                if (bounds.Top + deltaY < 0)
                    deltaY = bounds.Top * -1;
                if (bounds.Right + deltaX > parent.ClientRectangle.Width)
                    deltaX = parent.ClientRectangle.Width - bounds.Right;
                if (bounds.Bottom + deltaY > parent.ClientRectangle.Height)
                    deltaY = parent.ClientRectangle.Height - bounds.Bottom;
 
                foreach (Control c in SelectedControls)
                {
                    c.Location = new Point(c.Left + deltaX, c.Top + deltaY);
                }
 
                if (deltaX != 0 || deltaY != 0)
                {
                    moved = true;
                }
            }
        }
 
        //
        // Display sizing handles around picked control once dragging has completed
        //
        private void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            if (dragging && !moved) RemoveControl((Control)sender);
 
            dragging = false;
            moved = false;
            MoveHandles();
            ShowHandles();
        }
    }
 
Regards,
 
Diego
 
modified on Friday, December 26, 2008 10:49 PM

GeneralRe: Adaptation for multiple selection (more than one selected control). Pinmemberammar7922:12 26 Jan '09  
GeneralRe: Adaptation for multiple selection (more than one selected control). PinmemberDiego Osorio1:23 27 Jan '09  
GeneralDragging between containers Pinmemberdkalyan18:30 12 May '08  
GeneralGreat Work BUT !!!! Pinmemberzooooooz2:11 10 May '08  
GeneralResize Control @ Runtime Pinmemberferozasi12:00 22 Apr '08  
GeneralMoving with keyboard Question Pinmemberblakadm23:53 13 Apr '08  
GeneralRe: Moving with keyboard Question Pinmemberjazzyvishal19:51 21 Jul '08  
Questionhow labels are selected which are created at button click event Pinmemberjassi4u19:43 2 Aug '07  
GeneralNice Article!!! PinmemberIrfan Kothari0:32 11 Apr '07  
Questionhow about rotating ? PinmemberThe Code Guru13:25 19 Jan '07  
AnswerRe: how about rotating ? PinmemberJK Rajesh18:59 30 Jan '07  
AnswerRe: how about rotating ? PinmemberThe Code Guru20:14 30 Jan '07  
GeneralVS 2005 PinmemberNewbieDude0:59 7 Feb '06  
GeneralRe: VS 2005 Pinmembershitfish5:03 18 Aug '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 30 Sep 2003
Article Copyright 2003 by Jim Korovessis
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid