65.9K
CodeProject is changing. Read more.
Home

Resizable Controls at Runtime!

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.75/5 (23 votes)

Apr 18, 2004

CPOL
viewsIcon

59789

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.

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