5,276,156 members and growing! (21,718 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Simple Runtime Control Sizing and Dragging Class

By Jim Korovessis

Sample and brief description of simple class that enables sizing and dragging of controls on a form
C#, Windows, .NET, Visual Studio, Dev

Posted: 29 Sep 2003
Updated: 29 Sep 2003
Views: 77,413
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
57 votes for this Article.
Popularity: 8.10 Rating: 4.62 out of 5
2 votes, 3.5%
1
0 votes, 0.0%
2
1 vote, 1.8%
3
9 votes, 15.8%
4
45 votes, 78.9%
5

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



Location: United States United States

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralDragging between containersmemberdkalyan19:30 12 May '08  
GeneralGreat Work BUT !!!!memberzooooooz3:11 10 May '08  
GeneralResize Control @ Runtimememberferozasi13:00 22 Apr '08  
GeneralMoving with keyboard Questionmemberblakadm0:53 14 Apr '08  
Questionhow labels are selected which are created at button click eventmemberjassi4u20:43 2 Aug '07  
GeneralNice Article!!!memberIrfan Kothari1:32 11 Apr '07  
Generalhow about rotating ?memberThe Code Guru14:25 19 Jan '07  
GeneralRe: how about rotating ?memberJK Rajesh19:59 30 Jan '07  
AnswerRe: how about rotating ?memberThe Code Guru21:14 30 Jan '07  
GeneralVS 2005memberNewbieDude1:59 7 Feb '06  
GeneralRe: VS 2005membershitfish6:03 18 Aug '06  
GeneralUserControlsmemberdchurch241:04 24 Jan '06  
GeneralAdditional code for handling child controlsmemberRonit H5:01 31 Oct '05  
GeneralRe: Additional code for handling child controlsmemberheckknow16:54 2 Jan '07  
GeneralVB.NET code for us non-C# folks :)membermpemberton10:03 27 Oct '05  
GeneralSaving LayoutmemberkumagKayo18:06 22 Aug '05  
GeneralSuggestionmemberCarl Mercier11:35 11 Jul '05  
GeneralWorks great with my Custom Control + How to Unwire in Run Timememberbabuntu13:15 24 Jun '05  
GeneralNot able to Work with runtime Control and resizingmemberdvsriram21:22 18 Sep '07  
Generallimited to certain controls?memberdjhart19:18 8 May '05  
GeneralRe: limited to certain controls?memberMarkDuncan4:55 25 Jul '05  
GeneralRe: limited to certain controls?memberMarkDuncan4:59 25 Jul '05  
GeneralIncreasing productivitymemberIFreezy1:37 19 Apr '05  
GeneralRe: Increasing productivitymemberyoussef13:23 29 May '05  
GeneralRe: Increasing productivitymemberyoussef13:24 29 May '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Sep 2003
Editor: Nishant Sivakumar
Copyright 2003 by Jim Korovessis
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project