Click here to Skip to main content
12,893,452 members (56,107 online)
Click here to Skip to main content
Add your own
alternative version


96 bookmarked
Posted 29 Sep 2003

Simple Runtime Control Sizing and Dragging Class

, 29 Sep 2003 Public Domain
Rate this:
Please Sign up or sign in to vote.
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


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
    // Provide a Click event handler for each control
    // that attaches a pick box to the control when clicked
    foreach (Control c in this.Controls) {

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++) {
    //Position sizing handles around Control
    //Display sizing handles
    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.


  • This is the initial submission of the sample.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Jim Korovessis
Program Manager General Dynamics Canada
Canada Canada
Manager, Customer Training in Ottawa, ON, Canada

You may also be interested in...


Comments and Discussions

QuestionGreat patent - Any license? Pin
Member 76904269-Oct-11 13:50
memberMember 76904269-Oct-11 13:50 
AnswerRe: Great patent - Any license? Pin
Jim Korovessis11-Oct-11 1:39
memberJim Korovessis11-Oct-11 1:39 
GeneralMy vote of 5 Pin
Bharat Mallapur14-Apr-11 1:01
memberBharat Mallapur14-Apr-11 1:01 
AnswerUpdated with multiple selection Pin
ecklerpa21-Apr-09 9:17
memberecklerpa21-Apr-09 9:17 
GeneralPickBox and Pictureboxes Pin
Juwi_uk4-Mar-09 5:04
memberJuwi_uk4-Mar-09 5:04 
GeneralMultiple controls drag n drop. [modified] Pin
VB 8.020-Feb-09 1:45
memberVB 8.020-Feb-09 1:45 
GeneralGreate code Pin
ammar7926-Jan-09 22:04
memberammar7926-Jan-09 22:04 
GeneralAdaptation for multiple selection (more than one selected control). [modified] Pin
Diego Osorio26-Dec-08 16:43
memberDiego Osorio26-Dec-08 16:43 
GeneralRe: Adaptation for multiple selection (more than one selected control). Pin
ammar7926-Jan-09 22:12
memberammar7926-Jan-09 22:12 
GeneralRe: Adaptation for multiple selection (more than one selected control). Pin
Diego Osorio27-Jan-09 1:23
memberDiego Osorio27-Jan-09 1:23 
GeneralDragging between containers Pin
dkalyan12-May-08 18:30
memberdkalyan12-May-08 18:30 
GeneralGreat Work BUT !!!! Pin
zooooooz10-May-08 2:11
memberzooooooz10-May-08 2:11 
GeneralResize Control @ Runtime Pin
ferozasi22-Apr-08 12:00
memberferozasi22-Apr-08 12:00 
GeneralMoving with keyboard Question Pin
blakadm13-Apr-08 23:53
memberblakadm13-Apr-08 23:53 
GeneralRe: Moving with keyboard Question Pin
jazzyvishal21-Jul-08 19:51
memberjazzyvishal21-Jul-08 19:51 
Questionhow labels are selected which are created at button click event Pin
jassi4u2-Aug-07 19:43
memberjassi4u2-Aug-07 19:43 
GeneralNice Article!!! Pin
Irfan Kothari11-Apr-07 0:32
memberIrfan Kothari11-Apr-07 0:32 
Questionhow about rotating ? Pin
The Code Guru19-Jan-07 13:25
memberThe Code Guru19-Jan-07 13:25 
AnswerRe: how about rotating ? Pin
JK Rajesh30-Jan-07 18:59
memberJK Rajesh30-Jan-07 18:59 
AnswerRe: how about rotating ? Pin
The Code Guru30-Jan-07 20:14
memberThe Code Guru30-Jan-07 20:14 
GeneralVS 2005 Pin
NewbieDude7-Feb-06 0:59
memberNewbieDude7-Feb-06 0:59 
GeneralRe: VS 2005 Pin
shitfish18-Aug-06 5:03
membershitfish18-Aug-06 5:03 
GeneralUserControls Pin
dchurch2424-Jan-06 0:04
memberdchurch2424-Jan-06 0:04 
GeneralAdditional code for handling child controls Pin
Ronit H31-Oct-05 4:01
memberRonit H31-Oct-05 4:01 
GeneralRe: Additional code for handling child controls Pin
heckknow2-Jan-07 15:54
memberheckknow2-Jan-07 15:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 30 Sep 2003
Article Copyright 2003 by Jim Korovessis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid