![]() |
Languages »
C# »
Windows Forms
Intermediate
License: The GNU General Public License (GPL)
Runtime resizable controls!By Murat YILMAZAdd resizeability to your Windows Forms controls. |
C#.NET 1.0, .NET 1.1, Win2K, WinXP, WinForms, VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

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. :)
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 :) : ![]()
Put the image at the bottom right of the Panel control. It should look like:

And set the following properties of the Image control:
Image: Select the image.
Cursor: SizeNWSE.
Anchor: Bottom, Right. 
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.
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:
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 ;)

You can get the mouse location in the MouseMove event of any control as an integer.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Feb 2006 Editor: Smitha Vijayan |
Copyright 2006 by Murat YILMAZ Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |