Click here to Skip to main content
13,410,869 members (32,109 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 25 May 2007

Creating/Moving/Sizing Controls at Runtime

, 25 May 2007
Rate this:
Please Sign up or sign in to vote.
How to create and manage controls at runtime by providing the user with 'handles' for sizing the controls.

Screenshot - RuntimeResizer.png


Recently, looking for a VB.NET Form Creator, I found a professional control for ~$140 that added resize handles to controls on a form. So I decided to see whether I could duplicate the functionality without spending that much money. Also, I was looking for a way to add unlimited controls to the form that also had event handlers. The resultant combination is here for you to see.

Using the code

In the code, I have defined five labels (H_Top_Right, H_Top_Left, H_Bottom_Left, H_Bottom_Right, Mover) that act as the handles. The Drag1-Drag4 and isMoving variables keep track of whether the particular handle is in drag mode or not. This variable is set to 'True' on mouseDown and 'False' on mouseUp. The SelControl variable keeps track of the currently focused control. The SetActive() method is used to set the focused control.

The five handles change the SelControl's left, top, height, and width properties accordingly:

Private Sub H_Top_Left_Click (ByVal sender As System.Object, _
          ByVal e As System.Windows.Forms.MouseEventArgs) _
          Handles H_Top_Left.MouseMove
    If Not Drag1 then Exit Sub
    Me.H_Top_Left.Left += e.X
    Me.H_Top_Left.Top += e.Y
    SelControl.Width += -(e.X)
    SelControl.Height += -(e.Y)
    SelControl.Top = H_Top_Left.Top + 4
    SelControl.Left = H_Top_Left.Left + 4
End Sub

Private Sub H_Top_Right_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles H_Top_Right.MouseMove
    If Not Drag2 then Exit Sub
    Me.H_Top_Right.Left += e.X
    Me.H_Top_Right.Top += e.Y
    SelControl.Width += e.X
    SelControl.Height += -(e.Y)
    SelControl.Top = H_Top_Right.Top + 4
End Sub

Private Sub H_Bottom_Right_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles H_Bottom_Right.MouseMove
    If Not Drag3 then Exit Sub
    Me.H_Bottom_Right.Left += e.X
    Me.H_Bottom_Right.Top += e.Y
    SelControl.Width += e.X
    SelControl.Height += e.Y
End Sub

Private Sub H_Bottom_Left_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles H_Bottom_Left.MouseMove
    If Not Drag4 then Exit Sub
    Me.H_Bottom_Left.Left += e.X
    Me.H_Bottom_Left.Top += e.Y
    SelControl.Width += -(e.X)
    SelControl.Height += e.Y
    SelControl.Left = H_Bottom_Left.Left + 4
End Sub

Private Sub Mover_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles Mover.MouseMove
    If Not isMoving then Exit Sub
    Me.Mover.Left += e.X
    Me.Mover.Top += e.Y
    SelControl.Top = ((Me.Mover.Top + 4) * 2 - SelControl.Height) /2
    SelControl.Left = ((Me.Mover.Left + 4) * 2 - SelControl.Width) /2
End Sub

Here the position of all five handles is calculated according to the current position of the control.

The SetActive() method sets SelControl = the receiving button, and positions the handles accordingly.

Private Sub SetActive (ByVal button as System.Object)

    SelControl = button

    Me.H_Top_Left.Left = button.Left - 4
    Me.H_Top_Left.Top = button.Top - 4

    Me.H_Top_Right.Left = button.Left + button.Width
    Me.H_Top_Right.Top = button.Top - 4

    Me.H_Bottom_Right.Left = button.Left + button.Width
    Me.H_Bottom_Right.Top = button.Top + button.Height 

    Me.H_Bottom_Left.Left = button.Left - 4
    Me.H_Bottom_Left.Top = button.Top + button.Height 

    Me.Mover.Left = (button.Left + button.Left + Button.Width) /2 - 4
    Me.Mover.Top = (Button.Top + Button.Top + Button.Height) /2 - 4

End Sub


Yep! Every code has its bugs! Since this is my first attempt at any such thing, I was bound to make a few slips. To list them:

  • The Move method has not yet been implemented, i.e., you cannot drag buttons like in VS. You can only move them using the handlers.
  • Updated: You can now move the control using the white square in the centre of the control.

  • The top left handler, if dragged below the bottom of the button or to the right of the button, moves the entire button. This means that the button moves with 0 height/width and moves to the current mouse position. Irritating.

I'll try to wrinkle out those bugs in time. Suggestions and tips are welcome.

I'll also try to add a few more control types (e.g., ComboBox, CheckBox, RadioButton etc.) for the demo.


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


About the Author

Software Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThank You! Pin
cannalane23-Apr-09 18:51
membercannalane23-Apr-09 18:51 
Questionrun time buttons saving Pin
abidaarif16-Feb-09 17:04
memberabidaarif16-Feb-09 17:04 
AnswerRe: run time buttons saving Pin
Soumya9216-Feb-09 23:33
memberSoumya9216-Feb-09 23:33 
QuestionRe: run time buttons saving Pin
abidaarif24-Feb-09 16:23
memberabidaarif24-Feb-09 16:23 
GeneralThank You Very Much Pin
blackbe323-Jul-08 6:12
memberblackbe323-Jul-08 6:12 
QuestionQuestion Pin
BetaNium30-Nov-07 14:03
memberBetaNium30-Nov-07 14:03 
GeneralThank you Pin
jedimcclain30-Nov-07 7:37
memberjedimcclain30-Nov-07 7:37 
GeneralJust a help Pin
clins20-Sep-07 14:15
memberclins20-Sep-07 14: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180225.1 | Last Updated 25 May 2007
Article Copyright 2007 by Soumya92
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid