Click here to Skip to main content
11,409,097 members (65,018 online)
Click here to Skip to main content

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

Comments and Discussions

QuestionDoes PicBox works for the custom Usercontrols PinmemberSharmaShruthi7-Oct-13 21:10 
AnswerRe: Does PicBox works for the custom Usercontrols PinmemberJim Korovessis20-Oct-13 9:19 
Generalplease upload the source project PingroupTricube21-May-13 20:21 
QuestionThanks! PinmemberMember 96500768-Apr-13 10:59 
QuestionConstrain Proportions PinmemberMember 773856116-Jun-12 6:10 
QuestionHow to create multiple selection on panel. Pinmembertinku313-May-12 22:04 
GeneralThank Very Much Pinmembersanaz13x6-Jan-12 0:37 
SuggestionGreat article PinmemberSudarshan.chandan@gs.com13-Oct-11 23:34 
GeneralRe: Great article Pinmembersteffen_dec4-Jun-12 4:38 
QuestionGreat patent - Any license? PinmemberMember 76904269-Oct-11 14:50 
AnswerRe: Great patent - Any license? PinmemberJim Korovessis11-Oct-11 2:39 
GeneralMy vote of 5 PinmemberBharat Mallapur14-Apr-11 2:01 
AnswerUpdated with multiple selection Pinmemberecklerpa21-Apr-09 10:17 
GeneralPickBox and Pictureboxes PinmemberJuwi_uk4-Mar-09 6:04 
Hi all.

I have the great PickBox class working in my VB Project but I have one problem.

When expanding my picturebox the box scales but the image doesn't.

I had a similar command then did work before I moved to PickBox that basically did:

picturebox1.SizeMode = PictureBoxSizeMode.StretchImage

but sizemode isn't available as attribute of m_Control.

Can anyone advise at how I can get the image to scale too?

Thanks in advance.

GeneralMultiple controls drag n drop. [modified] PinmemberVB 8.020-Feb-09 2:45 
GeneralGreate code Pinmemberammar7926-Jan-09 23:04 
GeneralAdaptation for multiple selection (more than one selected control). [modified] PinmemberDiego Osorio26-Dec-08 17:43 
GeneralRe: Adaptation for multiple selection (more than one selected control). Pinmemberammar7926-Jan-09 23:12 
GeneralRe: Adaptation for multiple selection (more than one selected control). PinmemberDiego Osorio27-Jan-09 2:23 
GeneralDragging between containers Pinmemberdkalyan12-May-08 19:30 
GeneralGreat Work BUT !!!! Pinmemberzooooooz10-May-08 3:11 
GeneralResize Control @ Runtime Pinmemberferozasi22-Apr-08 13:00 
GeneralMoving with keyboard Question Pinmemberblakadm14-Apr-08 0:53 
GeneralRe: Moving with keyboard Question Pinmemberjazzyvishal21-Jul-08 20:51 
Questionhow labels are selected which are created at button click event Pinmemberjassi4u2-Aug-07 20:43 
GeneralNice Article!!! PinmemberIrfan Kothari11-Apr-07 1:32 
Questionhow about rotating ? PinmemberThe Code Guru19-Jan-07 14:25 
AnswerRe: how about rotating ? PinmemberJK Rajesh30-Jan-07 19:59 
AnswerRe: how about rotating ? PinmemberThe Code Guru30-Jan-07 21:14 
GeneralVS 2005 PinmemberNewbieDude7-Feb-06 1:59 
GeneralRe: VS 2005 Pinmembershitfish18-Aug-06 6:03 
GeneralUserControls Pinmemberdchurch2424-Jan-06 1:04 
GeneralAdditional code for handling child controls PinmemberRonit H31-Oct-05 5:01 
GeneralRe: Additional code for handling child controls Pinmemberheckknow2-Jan-07 16:54 
GeneralVB.NET code for us non-C# folks :) Pinmembermpemberton27-Oct-05 10:03 
GeneralSaving Layout PinmemberkumagKayo22-Aug-05 18:06 
GeneralSuggestion PinmemberCarl Mercier11-Jul-05 11:35 
GeneralWorks great with my Custom Control + How to Unwire in Run Time Pinmemberbabuntu24-Jun-05 13:15 
GeneralNot able to Work with runtime Control and resizing Pinmemberdvsriram18-Sep-07 21:22 
Questionlimited to certain controls? Pinmemberdjhart8-May-05 19:18 
AnswerRe: limited to certain controls? PinmemberMarkDuncan25-Jul-05 4:55 
GeneralRe: limited to certain controls? PinmemberMarkDuncan25-Jul-05 4:59 
GeneralIncreasing productivity PinmemberIFreezy19-Apr-05 1:37 
GeneralRe: Increasing productivity Pinmemberyoussef29-May-05 13:23 
GeneralRe: Increasing productivity Pinmemberyoussef29-May-05 13:24 
GeneralKilled 2 birds w/ one stone PinmemberKeyser_WS67-Feb-05 12:25 
GeneralRe: Killed 2 birds w/ one stone Pinsussik27-Feb-05 13:47 
GeneralGreat Control PinmemberCCosgrove18-Jan-05 20:18 
GeneralRe: Great Control PinsussAnonymous21-Jan-05 14:22 
GeneralRe: Great Control PinmemberKaieteur Potaro1-Nov-06 16:41 

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.

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