Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Simple Runtime Control Sizing and Dragging Class

Rate me:
Please Sign up or sign in to vote.
4.90/5 (73 votes)
29 Sep 2003Public Domain2 min read 226.7K   5.6K   99   65
Sample and brief description of simple class that enables sizing and dragging of controls on a form

Image 1

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:

C#
//(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.

C#
//(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, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Program Manager General Dynamics Mission Systems Canada
Canada Canada
Manager, Customer Training in Ottawa, ON, Canada
www.gdcanada.com

Comments and Discussions

 
GeneralKilled 2 birds w/ one stone Pin
Keyser_WS67-Feb-05 11:25
Keyser_WS67-Feb-05 11:25 
GeneralRe: Killed 2 birds w/ one stone Pin
jkorovessis7-Feb-05 12:47
jkorovessis7-Feb-05 12:47 
GeneralGreat Control Pin
CCosgrove18-Jan-05 19:18
CCosgrove18-Jan-05 19:18 
GeneralRe: Great Control Pin
Anonymous21-Jan-05 13:22
Anonymous21-Jan-05 13:22 
GeneralRe: Great Control Pin
Kaieteur Potaro1-Nov-06 15:41
Kaieteur Potaro1-Nov-06 15:41 
GeneralIt's the thing that I've found for along time Pin
Tung Nguyen14-Nov-04 20:24
Tung Nguyen14-Nov-04 20:24 
GeneralRe: It's the thing that I've found for along time Pin
dgudat8-Jun-05 7:10
dgudat8-Jun-05 7:10 
GeneralYou may use Remove() in... Pin
Ever_Rolling10-Sep-04 2:44
Ever_Rolling10-Sep-04 2:44 
The sample Client App don't use PickBox.Remove().
This may be added in a Mouse_Click EventHandler of the Parent Form.

Great work anyway.
Thanks
GeneralJust what I needed Pin
Neil Baliga18-Jul-04 7:20
Neil Baliga18-Jul-04 7:20 
QuestionDoes anyone have the VB 6 Version? Pin
cepolsonaz16-Apr-04 19:40
cepolsonaz16-Apr-04 19:40 
GeneralDragging between containers Pin
PaleyX17-Mar-04 9:52
PaleyX17-Mar-04 9:52 
GeneralRe: Dragging between containers Pin
Carl Mercier15-Jun-05 10:57
Carl Mercier15-Jun-05 10:57 
GeneralDeeply Appreciated Pin
Chieu Luu21-Nov-03 9:11
Chieu Luu21-Nov-03 9:11 
GeneralRe: Deeply Appreciated Pin
jkorovessis3-Dec-03 14:31
jkorovessis3-Dec-03 14:31 
GeneralHow to UnWire controls at runtime Pin
mahesh kumar s2-Feb-04 20:06
mahesh kumar s2-Feb-04 20:06 
GeneralRe: How to UnWire controls at runtime Pin
mahesh kumar s2-Feb-04 20:49
mahesh kumar s2-Feb-04 20:49 
GeneralRe: Deeply Appreciated Pin
kmaNick24-Jan-05 9:44
kmaNick24-Jan-05 9:44 
GeneralUnWireAllControl Code Pin
playyuer25-Oct-04 23:58
playyuer25-Oct-04 23:58 
GeneralUnable to select multiple controls Pin
Raju4jobs25-Jan-08 0:30
Raju4jobs25-Jan-08 0:30 

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.