Click here to Skip to main content
Click here to Skip to main content

Resizable Controls at Runtime!

By , 17 Apr 2004
 

Introduction

These simple bits of code will show you how to allow your users to resize a Label at runtime. Simply paste this code into an empty form (below the Windows generated stuff) and draw a Label (called Label1) anywhere on the form.

Run the program and notice the cursor change when your mouse is over the right side of the Label. Click and drag and watch the Label resize. This can be used for almost any control, and the code can be easily changed to allow width resizing as well.

Dim _mouseDown as boolean = false

Private Sub Label1_MouseMove(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove

   If _mouseDown = True Then
      Label1.Width = e.X
   Else

      If e.X >= Label1.Width - 3 Then
         Label1.Cursor = Cursors.VSplit
      Else
         Label1.Cursor = Me.Cursor
      End If

   End If

End Sub

Private Sub Label1_MouseDown(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown

   _mouseDown = True
   Label1.Cursor = Cursors.VSplit
End Sub

Private Sub Label1_MouseUp(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp

   _mouseDown = False
   Label1.Cursor = Me.Cursor
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Phylum
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWidth and Height, both are resizeable.memberKrrrishna19 Jun '06 - 5:50 
Now Width and Height, both are resizable.
 
with this code,
 
bool _mouseDownForWidth = false , _mouseDownForHeight = false;
const int _minLength = 35 , _minHeight = 20;
private System.Windows.Forms.Button button1;
const int _margin = 3;
private void button1_mouseDown(object sender, MouseEventArgs e)
{
if (e.X >= button1.Width - _margin && e.X <= button1.Width + _margin)
{
_mouseDownForWidth = true;
button1.Cursor = Cursors.SizeWE;
}
if (e.Y >= button1.Height - _margin && e.Y <= button1.Height + _margin)
{
_mouseDownForHeight = true;
button1.Cursor = Cursors.SizeNS;
}
if (_mouseDownForWidth == true && _mouseDownForHeight == true)
{
button1.Cursor = Cursors.SizeNWSE;
}
}
 
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDownForWidth == true)
{
button1.Width = (e.X < _minLength ? _minLength : e.X);
}
if (_mouseDownForHeight == true)
{
button1.Height = (e.Y < _minHeight ? _minHeight : e.Y);
}
if (_mouseDownForWidth == false && _mouseDownForHeight == false)
{
if (e.X >= button1.Width - _margin && e.Y >= button1.Height - _margin)
{
button1.Cursor = Cursors.SizeNWSE;
}
else
{
if (e.X >= button1.Width - _margin)
button1.Cursor = Cursors.SizeWE;
else
{
if (e.Y >= button1.Height - _margin)
button1.Cursor = Cursors.SizeNS;
else
button1.Cursor = this.Cursor;
}
}
}
}
 
private void button1_MouseUp(object sender, MouseEventArgs e)
{
_mouseDownForWidth = false;
_mouseDownForHeight = false;
button1.Cursor = this.Cursor;
}
 
"Today is mine, Tomorrow of no ones"

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 18 Apr 2004
Article Copyright 2004 by Phylum
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid