Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / Visual Basic

Resizable Controls at Runtime!

Rate me:
Please Sign up or sign in to vote.
2.75/5 (23 votes)
17 Apr 2004CPOL 59.2K   26   4
This simple code will allow users to resize controls at runtime.

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.

VB
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)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Global Analyser3-Nov-10 6:57
Global Analyser3-Nov-10 6:57 
GeneralWidth and Height, both are resizeable. Pin
Krrrishna19-Jun-06 5:50
Krrrishna19-Jun-06 5:50 
Generalcode in c#, Pin
Krrrishna25-May-06 20:43
Krrrishna25-May-06 20:43 
GeneralAll I can say is nice! Pin
Khookie4-Feb-06 3:10
Khookie4-Feb-06 3:10 

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.