A simple trick to resize a control at runtime

AID: 4274
  • Status: Published

7900 points

  • Bytrestan
  • TypeTips/Tricks
  • Posted on2010-12-21 at 13:30:24
Awards
  • Community Pick
  • Experts Exchange Approved
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods.
GUI1.png
  • 18 KB
  • Demo
Demo


Introduction

In one of my applications I needed a control that could be resizable at run time. Just like the canvas in MSPaint, you can resize it inside the host window. After a quick search on the internet I noticed that most solutions treating this simple issue as a beast.  An example of a "complex implementation" is in the EE Solution, here.

Using SendMessage is an intuitive temptation.  We could try the technique I descried in my previous article on how to drag a form -- wehe we simply flood mouse movement to the caption of the host form.  However, the parameters for resizing message scattered to eight directions and you'd have to calculate the delta in each direction to make it work.  That’s why the code tends to become so bulky.


Using PictureBox as decoration

When a control is runtime resizable, you'll certainly want to provide some visual indication to users. You can manually draw an “edge” on the control using graphic functions, and manually change the cursor type when the mouse moves into a range of control’s edge. It’s not hard to imagine that pages of source code might be needed to make it work smoothly.  I would rather simply use some control components to achieve the functionality.  My implementation is simple:

1. Place three PictureBox controls into the target control you want it to be resizable.
2. Arrange one picture box to the right edge of target control. Its height equals to the height of the
    target control minus 3. And set its anchor style as top, bottom, right.
3. Arrange the second box to the bottom edge of the target control. Its width equals to the width
    of the target control minus 3. And set its anchor style as left, right, bottom.
4. Place the third picture box at the right bottom corner of the target control. Both of its width
    and height is 3.  Set its anchor style as right, bottom.
5. Finally subscribe to the MouseMove of each picture box. Add the following codes in the
    event handler:
if (e.Button == System.Windows.Forms.MouseButtons.Left) 
{ 
    controltobeResized.Width += e.X; 
    controltobeResized.Height += e.Y; 
    if (controltobeResized.Width < decoration) 
    { 
        controltobeResized.Width = decoration; 
    } 
    if (controltobeResized.Height < decoration) 
    { 
        controltobeResized.Height = decoration; 
    } 
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window



Now in the runtime, when you move your mouse over a picture box, the cursor will change its shape accordingly.  So there is your visible user feedback.   Press down the left button to begin to resize the control.  The actual implementation is even easier than it sounds (see the attached code).


Reuse the code

Life is good, and it can be even better.

I put the operations described above in a separate class named as TCResize, which of course contains the three picture boxes and the hook to their events. The only thing you need to do is to include the class in your project and add one line of code to create an instance with the target control as parameter:
TCResize resizabletreeView = new TCResize(this.treeView1);
                                    
1:

Select allOpen in new window


The source code for the class, TCResize, is attached.


Extension Methods

In my application, I need to know the exact size of the target control.  While the decorative picture boxes occupied a little edge area of the target control, each time when I ask for the dimension information, I need to exclude the dimension of the decoration components. Prior to C# 3.0 (Visual Studio 2008), you may have to derive a class to override the build-in methods. Then it definitely breaks the concise way of reuse. Fortunately, the concept of an Extension Method (introduced in C# 2.0 and fully supported in C# 3.0) provides great flexibility to extent the behaviours of an “old time” class.

The syntax of creating Extension Methods is widely introduced on the Internet resources.  Briefly, you need to declare it as static methods in a top level static class. Using this keyword prefix to the type of the parameter passed into method.  In short: You can create extension methods on a base class and all its derived ones will pick up the methods automatically. The best part is that you can improve all of the derived classes simultaneously by extending their common base class!

Thus we add some get and set methods to the base Control class. The following code snippet is a sample method. Extension properties have not been supported.
public static int GetClientWidth(this Control theControl) 
        { 
            return theControl.Width - TCResize.Decoration; 
        }
                                    
1:
2:
3:
4:

Select allOpen in new window


Extension topics

Attached there is also a sample application TCPaint.exe (run on .net framework 3.5). It is developed to demonstrate several tricks published and those to be published. I originally just wanted to create something really simple like Windows mspaint.exe. After a couple of weeks of work, it has evolved into an application that provides unlimited steps of undo and redo, as well as editing of strokes and screen capture. The development work is still ongoing.  Bugs and butterflies are inevitable. It’s just attached to show the resizing trick in a real life application. After you draw a shape on the canvas, you can drag a bullet point (handle) to tune the shape.  They are also picture boxes.:)

Any comments on where this TCPaint program should lead to will be more than welcomed.

Code Attachments:
ResizableControl.zip
  • 10 KB
  • TCResize class source code and demo project
ResizableControl.zip
TCPaint.zip
  • 210 KB
  • A mspaint like software
TCPaint.zip
    Asked On
    2010-12-21 at 13:30:24ID4274
    Tags

    resize control

    ,

    runtime

    ,

    Extension Methods

    ,

    C#

    ,

    TCPaint

    Topic

    C# Programming Language

    Views
    2784

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top C# Experts

    1. kaufmed

      566,376

      Sage

      500 points yesterday

      Profile
      Rank: Genius
    2. BuggyCoder

      240,764

      Guru

      10 points yesterday

      Profile
      Rank: Sage
    3. navneethegde

      158,560

      Guru

      0 points yesterday

      Profile
      Rank: Wizard
    4. CodeCruiser

      140,708

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    5. TheLearnedOne

      137,350

      Master

      2,800 points yesterday

      Profile
      Rank: Savant
    6. wdosanjos

      124,511

      Master

      3,500 points yesterday

      Profile
      Rank: Genius
    7. AndyAinscow

      107,357

      Master

      0 points yesterday

      Profile
      Rank: Genius
    8. Dhaest

      98,335

      Master

      0 points yesterday

      Profile
      Rank: Genius
    9. Idle_Mind

      91,914

      Master

      0 points yesterday

      Profile
      Rank: Savant
    10. tommyBoy

      90,068

      Master

      0 points yesterday

      Profile
      Rank: Genius
    11. nishantcomp2512

      89,000

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    12. MlandaT

      86,553

      Master

      0 points yesterday

      Profile
      Rank: Genius
    13. Chinmay_Patel

      80,818

      Master

      0 points yesterday

      Profile
      Rank: Genius
    14. ddayx10

      66,538

      Master

      0 points yesterday

      Profile
      Rank: Sage
    15. anarki_jimbel

      66,132

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    16. ambience

      63,764

      Master

      0 points yesterday

      Profile
      Rank: Sage
    17. ukerandi

      62,593

      Master

      1,000 points yesterday

      Profile
      Rank: Guru
    18. apeter

      60,772

      Master

      0 points yesterday

      Profile
      Rank: Sage
    19. JamesBurger

      60,305

      Master

      0 points yesterday

      Profile
      Rank: Sage
    20. sedgwick

      52,750

      Master

      1,600 points yesterday

      Profile
      Rank: Genius
    21. emoreau

      50,917

      Master

      0 points yesterday

      Profile
      Rank: Genius
    22. ged325

      50,311

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    23. anuradhay

      49,977

      2,500 points yesterday

      Profile
      Rank: Guru
    24. techChallenger1

      47,638

      0 points yesterday

      Profile
      Rank: Guru
    25. mas_oz2003

      43,540

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame