VB.NET - How can we resize the controls in .net at run time.

Asked By Janardhan Bonthu on 09-Dec-08 08:13 AM
User should able to resize the controls at run time.

Allow the User to Resize Controls at Runtime - Binny ch replied to Janardhan Bonthu on 09-Dec-08 11:31 AM

will this code help you:

Public Class ResizeableControl
    Private WithEvents mControl As Control
    Private mMouseDown As Boolean = False
    Private mEdge As EdgeEnum = EdgeEnum.None
    Private mWidth As Integer = 4
    Private mOutlineDrawn As Boolean = False
    Private Enum EdgeEnum
        None
        Right
        Left
        Top
        Bottom
        TopLeft
    End Enum
    Public Sub New(ByVal Control As Control)
        mControl = Control
    End Sub
    Private Sub mControl_MouseDown(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles mControl.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            mMouseDown = True
        End If
    End Sub
    Private Sub mControl_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles mControl.MouseUp
        mMouseDown = False
    End Sub
    Private Sub mControl_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles mControl.MouseMove
        Dim c As Control = CType(sender, Control)
        Dim g As Graphics = c.CreateGraphics
        Select Case mEdge
            Case EdgeEnum.TopLeft
                g.FillRectangle(Brushes.Fuchsia, _
            0, 0, mWidth * 4, mWidth * 4)
                mOutlineDrawn = True
            Case EdgeEnum.Left
                g.FillRectangle(Brushes.Fuchsia, _
            0, 0, mWidth, c.Height)
                mOutlineDrawn = True
            Case EdgeEnum.Right
                g.FillRectangle(Brushes.Fuchsia, _
            c.Width - mWidth, 0, c.Width, c.Height)
                mOutlineDrawn = True
            Case EdgeEnum.Top
                g.FillRectangle(Brushes.Fuchsia, _
            0, 0, c.Width, mWidth)
                mOutlineDrawn = True
            Case EdgeEnum.Bottom
                g.FillRectangle(Brushes.Fuchsia, _
            0, c.Height - mWidth, c.Width, mWidth)
                mOutlineDrawn = True
            Case EdgeEnum.None
                If mOutlineDrawn Then
                    c.Refresh()
                    mOutlineDrawn = False
                End If
        End Select
        If mMouseDown And mEdge <> EdgeEnum.None Then
            c.SuspendLayout()
            Select Case mEdge
                Case EdgeEnum.TopLeft
                    c.SetBounds(c.Left + e.X, c.Top + e.Y, _
            c.Width, c.Height)
                Case EdgeEnum.Left
                    c.SetBounds(c.Left + e.X, c.Top, _
            c.Width - e.X, c.Height)
                Case EdgeEnum.Right
                    c.SetBounds(c.Left, c.Top, _
            c.Width - (c.Width - e.X), c.Height)
                Case EdgeEnum.Top
                    c.SetBounds(c.Left, c.Top + e.Y, _
            c.Width, c.Height - e.Y)
                Case EdgeEnum.Bottom
                    c.SetBounds(c.Left, c.Top, _
            c.Width, c.Height - (c.Height - e.Y))
            End Select
            c.ResumeLayout()
        Else
            Select Case True
                Case e.X <= (mWidth * 4) And _
            e.Y <= (mWidth * 4) 'top left corner
                    c.Cursor = Cursors.SizeAll
                    mEdge = EdgeEnum.TopLeft
                Case e.X <= mWidth 'left edge
                    c.Cursor = Cursors.VSplit
                    mEdge = EdgeEnum.Left
                Case e.X > c.Width - (mWidth + 1) 'right edge
                    c.Cursor = Cursors.VSplit
                    mEdge = EdgeEnum.Right
                Case e.Y <= mWidth 'top edge
                    c.Cursor = Cursors.HSplit
                    mEdge = EdgeEnum.Top
                Case e.Y > c.Height - (mWidth + 1) 'bottom edge
                    c.Cursor = Cursors.HSplit
                    mEdge = EdgeEnum.Bottom
                Case Else 'no edge
                    c.Cursor = Cursors.Default
                    mEdge = EdgeEnum.None
            End Select
        End If
    End Sub
    Private Sub mControl_MouseLeave(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles mControl.MouseLeave
        Dim c As Control = CType(sender, Control)
        mEdge = EdgeEnum.None
        c.Refresh()
    End Sub
End Class
Go through this link hope it helps you:
http://www.codeproject.com/KB/dotnet/Resize_Control_at_Runtime.aspx

re - Web Star replied to Janardhan Bonthu on 09-Dec-08 10:52 PM

http://www.freevbcode.com/ShowCode.asp?ID=4349

try this - selva murugan replied to Janardhan Bonthu on 10-Dec-08 01:12 AM

this.textBox1.Size = new Size(300, 500);

Me.ComboBox1.Size = New Size(50, 10)

TRY THIS CODE - C_A P replied to Janardhan Bonthu on 10-Dec-08 01:52 AM
Here's a sample code to increase a button's, the control's, size each time you click on the button.
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             button1.Size = new Size(button1.Width + 5, button1.Height + 5);
  4.         }
  5.  
To change the location of the control, the following code may be useful: It changes the location by adding 5 pixels to both the button's X and Y coordinates.

  1.    private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             button1.Location = new Point(button1.Location.X + 5, button1.Location.Y + 5);
  4.         }

TRY THIS CODE - C_A P replied to Janardhan Bonthu on 10-Dec-08 01:58 AM
I found some code for vb6 to do it and this is a modified hacked up version for .net.

Create a new class and add this code to your class:


Code:
Public Class clsResize
    Private Shared l_sin_OrigFactorX As Single, _
                    l_sin_OrigFactorY As Single
    Private Shared l_sin_FontFactor As Single


    Public Shared Function Get_Column_Size(ByVal i_OrigSize As Integer) As String
        Try
            Dim Screens() As System.Windows.Forms.Screen = _
                    System.Windows.Forms.Screen.AllScreens

            Dim l_sin_OrigFactorX As Single
            Dim i_Xpixels As Integer
            Dim i_DesignX As Integer = 800

            i_Xpixels = Screens(0).Bounds.Width ' Set up the screen values
            l_sin_OrigFactorX = (i_Xpixels / i_DesignX) ' Determine scaling factors

            Get_Column_Size = (i_OrigSize * l_sin_OrigFactorX).ToString

        Catch e As Exception
            Call clsPublic.Error_Handler(Reflection.MethodInfo.GetCurrentMethod.Name, e.ToString)
        End Try
    End Function

    Public Shared Sub Change_Resolution(ByVal def_MyForm As Form, ByVal b_ResizeForm As Boolean)
        Try
            Dim Screens() As System.Windows.Forms.Screen = _
                    System.Windows.Forms.Screen.AllScreens

            If Not (Screens(0).Bounds.Height = 600) And Not (Screens(0).Bounds.Width = 800) Then
                Dim i_Xpixels As Integer, _
                    i_Ypixels As Integer 'For Screen Resolution

                Dim i_DesignX As Integer = 800, _
                    i_DesignY As Integer = 600

                ' Set up the screen values
                i_Ypixels = Screens(0).Bounds.Height
                i_Xpixels = Screens(0).Bounds.Width

                ' Determine scaling factors

                l_sin_OrigFactorX = (i_Xpixels / i_DesignX) + 0.001 'Added for better ratio
                l_sin_OrigFactorY = (i_Ypixels / i_DesignY) + 0.05 'Added to make the screen fill out better
                l_sin_FontFactor = ((l_sin_OrigFactorX + (i_Ypixels / i_DesignY)) / 2) * 0.9

                If b_ResizeForm = True Then
                    def_MyForm.Width = def_MyForm.Width * (i_Xpixels / i_DesignX)
                    def_MyForm.Height = def_MyForm.Height * (i_Ypixels / i_DesignY)
                    def_MyForm.Left = (Screens(0).Bounds.Width - def_MyForm.Width) / 2
                    def_MyForm.Top = (Screens(0).Bounds.Height - def_MyForm.Height) / 2
                End If

                Call Resize_For_Resolution(def_MyForm)
            End If

        Catch e As Exception
            Call clsPublic.Error_Handler(Reflection.MethodInfo.GetCurrentMethod.Name, e.ToString)
        End Try
    End Sub

    Public Shared Sub Resize_Encapsulations(ByVal cc_Controls As Control)
        Try

            Dim i_Encaps As Integer

            With cc_Controls
                For i_Encaps = 0 To cc_Controls.Controls.Count - 1
                    If TypeOf .Controls(i_Encaps) Is ComboBox Then ' cannot change Height
                        .Controls(i_Encaps).Left = .Controls(i_Encaps).Left * l_sin_OrigFactorX
                        .Controls(i_Encaps).Top = .Controls(i_Encaps).Top * l_sin_OrigFactorY
                        .Controls(i_Encaps).Width = .Controls(i_Encaps).Width * l_sin_OrigFactorX

                    ElseIf TypeOf .Controls(i_Encaps) Is Panel Then
                        .Controls(i_Encaps).Left = .Controls(i_Encaps).Left * l_sin_OrigFactorX
                        .Controls(i_Encaps).Top = .Controls(i_Encaps).Top * l_sin_OrigFactorY
                        .Controls(i_Encaps).Width = .Controls(i_Encaps).Width * l_sin_OrigFactorX
                        .Controls(i_Encaps).Height = .Controls(i_Encaps).Height * l_sin_OrigFactorY

                        Call Resize_Encapsulations(.Controls(i_Encaps))

                    ElseIf TypeOf .Controls(i_Encaps) Is Infragistics.Win.UltraWinTabControl.UltraTabPageControl Then
                        .Controls(i_Encaps).Left = .Controls(i_Encaps).Left * l_sin_OrigFactorX
                        .Controls(i_Encaps).Top = .Controls(i_Encaps).Top * l_sin_OrigFactorY
                        .Controls(i_Encaps).Width = .Controls(i_Encaps).Width * l_sin_OrigFactorX
                        .Controls(i_Encaps).Height = .Controls(i_Encaps).Height * l_sin_OrigFactorY

                        Call Resize_Encapsulations(.Controls(i_Encaps))

                    ElseIf TypeOf .Controls(i_Encaps) Is GroupBox Then
                        .Controls(i_Encaps).Left = .Controls(i_Encaps).Left * l_sin_OrigFactorX
                        .Controls(i_Encaps).Top = .Controls(i_Encaps).Top * l_sin_OrigFactorY
                        .Controls(i_Encaps).Width = .Controls(i_Encaps).Width * l_sin_OrigFactorX
                        .Controls(i_Encaps).Height = .Controls(i_Encaps).Height * l_sin_OrigFactorY

                        Call Resize_Encapsulations(.Controls(i_Encaps))
                    Else
                        .Controls(i_Encaps).Left = .Controls(i_Encaps).Left * l_sin_OrigFactorX
                        .Controls(i_Encaps).Top = .Controls(i_Encaps).Top * l_sin_OrigFactorY
                        .Controls(i_Encaps).Width = .Controls(i_Encaps).Width * l_sin_OrigFactorX
                        .Controls(i_Encaps).Height = .Controls(i_Encaps).Height * l_sin_OrigFactorY
                    End If

                    .Controls(i_Encaps).Font = New Font(.Controls(i_Encaps).Font.Name, .Controls(i_Encaps).Font.Size * l_sin_FontFactor)
                Next i_Encaps
            End With

        Catch e As Exception
            Call clsPublic.Error_Handler(Reflection.MethodInfo.GetCurrentMethod.Name, e.ToString)
        End Try
    End Sub

    Public Shared Sub Resize_For_Resolution(ByVal def_MyForm As Form)
        Try

            Dim i_i As Integer, _
                i_Grouboxes As Integer
            Dim sin_SFFont As Single

            sin_SFFont = (l_sin_OrigFactorX + l_sin_OrigFactorY) / 2 ' average scale

            ' Size the Controls for the new resolution
            'On Error Resume Next ' for read-only or nonexistent properties

            With def_MyForm
                For i_i = 0 To .Controls.Count - 1
                    If TypeOf .Controls(i_i) Is ComboBox Then ' cannot change Height
                        .Controls(i_i).Left = .Controls(i_i).Left * l_sin_OrigFactorX
                        .Controls(i_i).Top = .Controls(i_i).Top * l_sin_OrigFactorY
                        .Controls(i_i).Width = .Controls(i_i).Width * l_sin_OrigFactorX

                    ElseIf TypeOf .Controls(i_i) Is Panel Then
                        .Controls(i_i).Left = .Controls(i_i).Left * l_sin_OrigFactorX
                        .Controls(i_i).Top = .Controls(i_i).Top * l_sin_OrigFactorY
                        .Controls(i_i).Width = .Controls(i_i).Width * l_sin_OrigFactorX
                        .Controls(i_i).Height = .Controls(i_i).Height * l_sin_OrigFactorY

                        Call Resize_Encapsulations(.Controls(i_i))
                    ElseIf TypeOf .Controls(i_i) Is Infragistics.Win.UltraWinTabControl.UltraTabControl Then
                        .Controls(i_i).Left = .Controls(i_i).Left * l_sin_OrigFactorX
                        .Controls(i_i).Top = .Controls(i_i).Top * l_sin_OrigFactorY
                        .Controls(i_i).Width = .Controls(i_i).Width * l_sin_OrigFactorX
                        .Controls(i_i).Height = .Controls(i_i).Height * l_sin_OrigFactorY

                        Call Resize_Encapsulations(.Controls(i_i))

                    ElseIf TypeOf .Controls(i_i) Is GroupBox Then
                        .Controls(i_i).Left = .Controls(i_i).Left * l_sin_OrigFactorX
                        .Controls(i_i).Top = .Controls(i_i).Top * l_sin_OrigFactorY
                        .Controls(i_i).Width = .Controls(i_i).Width * l_sin_OrigFactorX
                        .Controls(i_i).Height = .Controls(i_i).Height * l_sin_OrigFactorY

                        Call Resize_Encapsulations(.Controls(i_i))
                    Else
                        .Controls(i_i).Left = .Controls(i_i).Left * l_sin_OrigFactorX
                        .Controls(i_i).Top = .Controls(i_i).Top * l_sin_OrigFactorY
                        .Controls(i_i).Width = .Controls(i_i).Width * l_sin_OrigFactorX
                        .Controls(i_i).Height = .Controls(i_i).Height * l_sin_OrigFactorY
                    End If

                    .Controls(i_i).Font = New Font(.Controls(i_i).Font.Name, .Controls(i_i).Font.Size * l_sin_FontFactor)
                Next i_i
            End With

        Catch e As Exception
            Call clsPublic.Error_Handler(Reflection.MethodInfo.GetCurrentMethod.Name, e.ToString)
        End Try
    End Sub
End Class


Now on a forms load, make a call like this:

Code:
Call clsResize.Change_Resolution(Me, False)
Re :: Resize the control at runtime in asp.net - SP replied to Janardhan Bonthu on 11-Dec-08 05:10 AM
http://www.codeguru.com/cpp/controls/controls/resizing/article.php/c2151#more




With a couple of lines of code, you can give your end-users the ability to resize any control.

Suppose you wanted to give the user the ability to modify the size and position on a certain control? This example shows how to implement resizing controls on a dialog box as it is done when drawing controls on a dialog template or visual basic at design time. In order to accomplish this we can use the class CRectTracker to manage all the drawing and resizing of a rectangular frame which also has (optional) 6 resize handlers (as shown in the image above). The first thing, you'll need to do is to invoke a CRectTracker and specify given coordinates:

LPRECT rect = new RECT;
CWnd* wnd = (CWnd*)(GetDlgItem(IDC_EDIT1));
wnd->GetWindowRect(rect) ;
ScreenToClient(rect) ;
m_tracker = new CRectTracker(rect, CRectTracker::dottedLine |
                                   CRectTracker::resizeOutside |
                                   CRectTracker::hatchedBorder);
m_tracker->Draw(pDC)  ;
There are only two events that are needed to be handled:
  • SetCursor
    if (pWnd == this && m_tracker->SetCursor(this, nHitTest))
     return TRUE;
    
    This is done in order to draw the correct mouse cursors when floating the mouse pointer over the rectangle.

  • LButtonDown
    m_tracker->Track(this, point, TRUE);
    Invalidate(FALSE);
    CDC* pDC = GetDC();
    m_tracker->Draw(pDC);
    
    This will take care of the drawing of the rectangle with resizing it.

    Once you finished all you have to do is draw the control with the new rectangle coordinates:

    LPRECT rect = new RECT;
    CWnd* wnd = (CWnd*)(GetDlgItem(IDC_EDIT1));
    rect = LPRECT(m_tracker->m_rect);
    wnd->MoveWindow(rect,TRUE) ;
    
Download the Code -  HERE
Correct Link To download the code - SP replied to SP on 11-Dec-08 05:13 AM
http://www.codeguru.com/code/legacy/controls/resize.zip
User should able to resize the controls at run time. - ulhas chaudhari replied to Janardhan Bonthu on 02-Jun-09 09:53 AM
end of post