Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Windows Forms
Article

Runtime resizable controls!

Rate me:
Please Sign up or sign in to vote.
4.61/5 (23 votes)
23 Feb 2006GPL32 min read 119.6K   9.2K   51   10
Add resizeability to your Windows Forms controls.

Resize me!

Introduction

Have you the need or have you tried to resize controls at runtime? Or did you ever have a client who wanted to enlarge or shrink controls on their programs? I once had a requirement like that. I searched for resizable controls on the internet, but I couldn't find any. I had to write some code to add resizeability to my controls. Here are some tricks to do these. This ability should be added to controls and the clients allowed to design their own forms. :)

Using the code

To add this ability to controls, we'll use some control properties and images. Firstly, create a Windows project (C#) and enlarge the startup form. Then add a Panel to the form.

Now, we need an image to put on our control's bottom right section to indicate that our control can resize. We will create a GIF file for this. You can copy (via screenshot) or redraw the image, or you can save the following image :) : Copy this image to inlude in your resizable control

Put the image at the bottom right of the Panel control. It should look like:

When you add image to control, it seems like this

And set the following properties of the Image control:

  • Image: Select the image.
  • Cursor: SizeNWSE.
  • Anchor: Bottom, Right.

Set the properties like this

At the code-behind, firstly we have to know that the mouse has been clicked or not. To know this, we can create a boolean variable and set it to true when the mouse is down and false when mouse is up.

C#
bool mouseClicked = false;

private void pictureBox1_MouseDown(object sender, 
             System.Windows.Forms.MouseEventArgs e)
{
    mouseClicked = true;
}

private void pictureBox1_MouseUp(object sender, 
        System.Windows.Forms.MouseEventArgs e)
{
    mouseClicked = false;
}

After doing it, we can write the resizeability code for the event when the mouse is over the image:

C#
private void pictureBox1_MouseMove(object sender, 
             System.Windows.Forms.MouseEventArgs e)
{
    if (mouseClicked)
    {
        this.panel1.Height = pictureBox1.Top + e.Y;
        this.panel1.Width = pictureBox1.Left + e.X;
    }
}

That's it. Lets run our project and see if the control has been added the ability. Have fun ;)

Resize me!

Points of Interest

You can get the mouse location in the MouseMove event of any control as an integer.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Product Manager Doğan Online
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralVery good! Pin
adrian_bogorin14-Jul-17 23:31
adrian_bogorin14-Jul-17 23:31 
Questionhow to resize Gridview similar to resizing picturebox ?? Pin
Mouni Bonasu23-Apr-15 23:55
Mouni Bonasu23-Apr-15 23:55 
GeneralThanks. Works very well. Pin
tigerwood200616-Oct-14 13:23
tigerwood200616-Oct-14 13:23 
AnswerThanks ! Pin
deveyehendekatlatanadam11-Jun-14 6:24
deveyehendekatlatanadam11-Jun-14 6:24 
QuestionRuntime resizable controls Pin
Member 1030123727-Oct-13 3:39
Member 1030123727-Oct-13 3:39 
AnswerCapture = true Pin
Oleksandr Kucherenko28-Feb-06 1:59
Oleksandr Kucherenko28-Feb-06 1:59 
GeneralRe: Capture = true Pin
Murat YILMAZ28-Feb-06 2:41
Murat YILMAZ28-Feb-06 2:41 
AnswerRe: Capture = true Pin
Oleksandr Kucherenko28-Feb-06 2:53
Oleksandr Kucherenko28-Feb-06 2:53 
simply try to move mouse out of form client area when you make resizing... when mouse pointer leave form area your become to work incorrectly...

steps to reproduce:
1) run sample
2) resize sample window to small enough size (not maximized mode)
3) start resizing process
4) drag mouse until you leave form area
5) you can now un-press mouse button
6) now try to move mouse back into form client area


Good Luck
Alex Kucherenko
GeneralMouseEventArgs.Button property Pin
Patric_J23-Feb-06 10:55
Patric_J23-Feb-06 10:55 
GeneralRe: MouseEventArgs.Button property Pin
Jesse Chunn1-Oct-07 9:15
Jesse Chunn1-Oct-07 9:15 

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.