Click here to Skip to main content
Click here to Skip to main content

Simple Runtime Control Sizing and Dragging Class

By , 29 Sep 2003
 

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanks!memberMember 96500768 Apr '13 - 9:59 
We are using your code in a GUI designer for a windows based game engine that we are making. It works very well and saved me a ton of time so thanks!   -JC
QuestionConstrain ProportionsmemberMember 773856116 Jun '12 - 5:10 
Hi loving the resize and move but is there anyway to constrain proportions   Many thanks   Rob
QuestionHow to create multiple selection on panel.membertinku313 May '12 - 21:04 
I am creating panel as form. Depending on particular panel i am adding controls to that panel .     like that i am adding some no of controls on panel .     How to create multiple selection ,Moving of controls at runtime like in Visual studio design time...
GeneralThank Very Muchmembersanaz13x5 Jan '12 - 23:37 
Thank Very Much My Dear . this Class Very Good For me.
SuggestionGreat articlememberSudarshan.chandan@gs.com13 Oct '11 - 22:34 
I added additional logic to be able to avoid collision and overlap. A bit of rectangle intersect logic in lbl and ctl MouseMove events. I had to also capture if the user action was to reduce the size or increase. works grt. thanks for your article.
GeneralRe: Great articlemembersteffen_dec4 Jun '12 - 3:38 
Hello,   can you please post your changes?   thanks steffen
QuestionGreat patent - Any license?memberMember 76904269 Oct '11 - 13:50 
Dear Jim,   Is there any license required to use this patent in a commercial package ?   Thank you.   Thai
AnswerRe: Great patent - Any license?memberJim Korovessis11 Oct '11 - 1:39 
Thai,   Please feel free to use all or any part of the code in any way that you wish. I did it as a hobbyist as a stand-alone example and not part of a software project, just to get a feel for NET technology. I hope it will be of help to you.   Good luck,   Jim
GeneralMy vote of 5memberBharat Mallapur14 Apr '11 - 1:01 
Too good! This is exactly what I was looking for! Plus the implementation looks well written.
AnswerUpdated with multiple selectionmemberecklerpa21 Apr '09 - 9:17 
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms;   namespace ScreenEditor { /// /// This class implements sizing and moving functions for /// runtime editing of graphic controls /// public...
GeneralPickBox and PictureboxesmemberJuwi_uk4 Mar '09 - 5: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.
 
Julian
GeneralMultiple controls drag n drop. [modified]memberVB 8.020 Feb '09 - 1:45 
Hi Jim,   Thanks for your great code . I try to create a selections for multiple controls when ctrl +click . i am creating instances for the pickbox and i am changing the color of label controls to identify .   But how can i delete the instance again...
GeneralGreate codememberammar7926 Jan '09 - 22:04 
This is just what i was looking for.   I just added a few events so that i could show the current size and location of the control that was moved.   Thanks!
GeneralAdaptation for multiple selection (more than one selected control). [modified]memberDiego Osorio26 Dec '08 - 16:43 
Hi all. Thank you Jim for your great work   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...
GeneralRe: Adaptation for multiple selection (more than one selected control).memberammar7926 Jan '09 - 22:12 
Hi Diego,   private List[Control] _SelectedControls = new List[Control]();   Invalid token '[' in class, struct, or interface member declaration   I am getting this error in VS2008. Can you help me, because i liked the multiple selection idea.   Regards, Ammar
GeneralRe: Adaptation for multiple selection (more than one selected control).memberDiego Osorio27 Jan '09 - 1:23 
Hi Ammar, please change "[" and "]" with "<" and ">". I wrote this way because the browser take my code as HTML when my post is shown. Now I know how to do it better   In the example: private List[Control] _SelectedControls = new List[Control](); after you change it: ...
GeneralDragging between containersmemberdkalyan12 May '08 - 18:30 
Thanks for a great piece of software. Would it be possible to change it so that a dragged control can be moved between containers, for example the label could be dropped into the panel or the panel into another panel?
GeneralGreat Work BUT !!!!memberzooooooz10 May '08 - 2:11 
thanks for your great work, but the problem is adding a Container control like a panel (level2) into a panel with another controls (level1), and then wiring the children controls of this panel. this way, when you click on a level1 control and then clicking on level2 control, you'll see that you...
GeneralResize Control @ Runtimememberferozasi22 Apr '08 - 12:00 
hi,   1. how can i put the Mouse_Right Click event for selection of a control while its now selecting on left mouse click i want to use Right mouse click for resize a control..   2. and how to put the control resize option under Tabs , pannel or subcontrols   MFS
GeneralMoving with keyboard Questionmemberblakadm13 Apr '08 - 23:53 
how do i implement this class using keyboard to move controls..any idea guys???thanks...
GeneralRe: Moving with keyboard Questionmemberjazzyvishal21 Jul '08 - 19:51 
Create a Keydown event under select Control method.   m_control.KeyDown += new KeyEventHandler(m_control_KeyDown);   and add this menthod .   private void m_control_KeyDown(object sender, KeyEventArgs e) { //left = 37,Up= 38,right= 39,down=40; ...
Questionhow labels are selected which are created at button click eventmemberjassi4u2 Aug '07 - 19:43 
when i created a label at runtime through button click .....although they are also controls but cannot b selected........can anybody help me
GeneralNice Article!!!memberIrfan Kothari11 Apr '07 - 0:32 
Hello sir,   Thanks a lot, i got exactly what i want..   Really a great article..   Thanks, Irfan   "Take Care Not Chances"
Questionhow about rotating ?memberThe Code Guru19 Jan '07 - 13:25 
how about impleminting a rotating of the control ? can it be done and how ?   Imagination is more important than knowledge... {Albert Einstein}
AnswerRe: how about rotating ?memberJK Rajesh30 Jan '07 - 18:59 
This is exactly what i need too..... i need to place the sizing handles at different rotation angles and add an extra sizing handle to rotate the control....if u hav any code for that then pls do help.   Thank you

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

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