Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / Windows Forms

Creating/Moving/Sizing Controls at Runtime

Rate me:
Please Sign up or sign in to vote.
3.47/5 (9 votes)
25 May 2007CPOL2 min read 40.5K   1.1K   37   8
How to create and manage controls at runtime by providing the user with 'handles' for sizing the controls.

Screenshot - RuntimeResizer.png

Introduction

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:

VB
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
    SetActive(SelControl)
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
    SetActive(SelControl)
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
    SetActive(SelControl)
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
    SetActive(SelControl)
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
    SetActive(SelControl)
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.

VB
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

Bugs

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.

License

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


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

Comments and Discussions

 
GeneralThank You! Pin
cannalane23-Apr-09 17:51
cannalane23-Apr-09 17:51 
Questionrun time buttons saving Pin
abidaarif16-Feb-09 16:04
abidaarif16-Feb-09 16:04 
how do we save the button which were created during runtime
AnswerRe: run time buttons saving Pin
Soumya9216-Feb-09 22:33
Soumya9216-Feb-09 22:33 
QuestionRe: run time buttons saving Pin
abidaarif24-Feb-09 15:23
abidaarif24-Feb-09 15:23 
GeneralThank You Very Much Pin
blackbe323-Jul-08 5:12
blackbe323-Jul-08 5:12 
QuestionQuestion Pin
BetaNium30-Nov-07 13:03
BetaNium30-Nov-07 13:03 
GeneralThank you Pin
jedimcclain30-Nov-07 6:37
jedimcclain30-Nov-07 6:37 
GeneralJust a help Pin
clins20-Sep-07 13:15
clins20-Sep-07 13: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.