How-To Allow the User to Resize Controls on a Form at Runtime
Introduction
In my previous postI showed how to let the user move a TextBox on the form at run-time using the mouse.
I am going to take this one step further and show how to let the user resize the TextBox when clicking and draging on the borders of the TextBox.
The Example
I only implemented resizing the width (left/right) of the control (This is a regular TextBox - no meaning to resizing vertically). You can apply the same algorithm to program vertical resizing yourself. The code is in VB.NET.
-----------------------------------------------
Private _mouseDown As Boolean = False
Private _initialMouseX As Integer
Private _initialMouseY As Integer
Private _initialWidth As Integer
Private Enum Borders
Left
Right
None
End Enum
Private _resizeType As Borders = Borders.None
Private Sub textBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles textBox1.MouseDown
_mouseDown = true
_resizeType = GetBorder(e.X, e.Y)
_initialMouseX = e.X
_initialMouseY = e.Y
_initialWidth = textBox1.Width
End Sub
Private Function GetBorder(int x, int y) As Borders
If (x<5 And x>-5) Then _
Return Borders.Left
If (x>textBox1.Width-10 And x<textBox1.Width+10) Then _
Return Borders.Right
Return Borders.None
End Function
Private Sub textBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles textBox1.MouseUp
_mouseDown = false
Me.Cursor = Cursors.Arrow
_resizeType = Borders.None
End Sub
Private Sub textBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles textBox1.MouseMove
If (Not _mouseDown) Then
If (GetBorder(e.X, e.Y) <> Borders.None) Then
Me.Cursor = Cursors.SizeWE
Else
Me.Cursor = Cursors.Arrow
End If
Return
End If
If (_resizeType = Borders.None) Then
Dim deltaX As Integer = e.X - _initialMouseX
Dim deltaY As Integer = e.Y - _initialMouseY
textBox1.Left += deltaX
textBox1.Top += deltaY
Else
Dim deltaX As Integer = e.X - _initialMouseX
Select Case _resizeType
Case Borders.Left:
If (textBox1.Width - deltaX>0) Then
textBox1.Left += deltaX
textBox1.Width -= deltaX
End If
Case Borders.Right:
If (_initialWidth + deltaX>0) Then _
textBox1.Width = _initialWidth + deltaX
End Select
End If
End Sub
Private Sub textBox1_MouseLeave(sender As Object, e As EventArgs) Handles textBox1.MouseLeave
Me.Cursor = Cursors.Arrow;
End Sub
--------------------------------------------
When the user click on the control we check if the mouse is on the border (GetBorder method) and sample the initial width of the control . The actual resize is done in the MouseMove event handler. It is done by changing the Width property of the control (calculated comparing the initial width).
4 Comments:
Thanks. it's realy helpful
it is too much great , you are really professional man
I edit your code and I put it in a class and make it movable and sizable and can add in run time
please let me know if you can help me to serialize that text box so I can save it in xml file
excellent code. Really helped.
PS. I have this code in the button code and set the content from textbox1 to Me
Public class MyButton
Inherits Button
--- Code---
End Class
hi,
thanks a lot for you'r great code, i was searching this for a long time.
i'd modified it in adding top and down moves and used this in usercontrol.
works perfect.
Post a Comment
<< Home