Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime
Resize Controls at runtime Resize Controls at runtime
Resize Controls at runtime
Go Back  Xtreme Visual Basic Talk > > > Resize Controls at runtime


Reply
 
Thread Tools Display Modes
  #1  
Old 09-27-2011, 09:39 AM
das18 das18 is offline
Newcomer
 
Join Date: Aug 2010
Posts: 3
Question Resize Controls at runtime


I have created a custom control in visual studio 2005. It contains a picture box with and image in it with 2 text boxes on top.
I want to be able to resize this image by selecting the edge and clicking and dragging it.

Please advise if you know a way to do this?
Reply With Quote
  #2  
Old 09-27-2011, 04:55 PM
snarfblam's Avatar
snarfblamResize Controls at runtime snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

I would want to detect when the cursor was at the edge of the control in question (I'm not sure whether you want to resize the PictureBox alone or the whole user-control). If the mouse button is pressed and held when the cursor is at the edge, I would monitor the mouse movements and adjust the size of the control accordingly. I might use the sizing cursors when the cursor is at the edge to indicate to the user when he has his mouse in the correct position to size the image.

Is there a particular step you are having trouble with? It might sound silly, but I find if you break a task down into excruciating detail, the code naturally follows.
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 09-28-2011, 03:02 AM
das18 das18 is offline
Newcomer
 
Join Date: Aug 2010
Posts: 3
Default

I want the how control to includine the picture box and text box to resize it.

I found this code on the internet but it does not resize the control:


Code:
Public Class ResizeableControl

        Private WithEvents mControl As Control
        Private mMouseDown As Boolean = False
        Private mEdge As EdgeEnum = EdgeEnum.None
        Private mWidth As Integer = 8
        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


This bit is under the button.click
Code:
Me.Controls.Add(Bin)
pbBin = Bin.pbBin
rc = New ResizeableControl(pbBin)

Please advise.
Reply With Quote
  #4  
Old 09-28-2011, 05:02 PM
snarfblam's Avatar
snarfblamResize Controls at runtime snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

It looks like you want a copy and paste solution. Did that code come with an explanation? How much effort did you make to understand the code? I'll be glad to help you out, but it will involve me dragging you through a drawn-out explanation of what all the code does and understanding the code will be requisite since I'm leaving half the code for you to write on your own.

So... First of all, let me make sure I understand right. You have a UserControl containing two text boxes above, and a PictureBox beneath. You want to be able to resize the image by dragging the edges (which edges?). Do you want the user to drag the PictureBox control or the containing UserControl (maybe there is a margin)? Does the UserControl need to resize with the PictureBox? Or is it assumed that the UserControl is already large enough? Specifics matter! I'm going to assume you want the user to drag on the PictureBox's right and bottom edges, and that the UserControl does not need to resize with the image.

If you haven't already, you want to specify "StretchImage" as the SizeMode of the PictureBox. I'll tackle horizontal resizing and leave the vertical resizing up to you.

First we need to figure out if the cursor is at the right edge. We'll declare a class-level variable to keep track. We'll also change the cursor when the user is in the right spot to drag the mouse.
Code:
    Dim AtRightEdge As Boolean = False
    Const DragMarginWidth As Integer = 6

    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        ' Is mouse within right six-or-so pixels?
        AtRightEdge = e.X > (PictureBox1.Width - DragMarginWidth)

        ' Set the cursor accordingly
        If (AtRightEdge) Then
            PictureBox1.Cursor = Cursors.SizeWE
        Else
            PictureBox1.Cursor = Cursors.Default
        End If
    End Sub
Now, if the user presses the mouse button when he is within the dragging margin, we need to begin dragging. We'll declare another class-level variable to track whether or not the user is dragging the mouse.

Code:
    Dim DraggingHorizontal As Boolean

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        ' If the user presses the mouse button at the right edge, begin dragging
        If AtRightEdge Then
            DraggingHorizontal = True
        End If
    End Sub
Now we need to implement the actual dragging. We need to watch how much the mouse moves, and adjust the size of the PictureBox accordingly. First we need to record the initial mouse position, which calls for another variable.
Code:
    Dim DraggingHorizontal As Boolean
    Dim DragOrigin As Point

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        ' If the user presses the mouse button at the right edge, begin dragging
        If AtRightEdge Then
            DraggingHorizontal = True
            DragOrigin = e.Location
        End If
    End Sub
Now, when the mouse moves, we adjust the size of the PictureBox. We already have some code in the MoseMove event, but that code won't need to be run while we are dragging the mouse.
Code:
    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If DraggingHorizontal Then
            ' How far did the mouse move? (Distance = newPoint - oldPoint)
            Dim movement As Point = New Point(e.X - DragOrigin.X, e.Y - DragOrigin.Y)

            ' Resize the image by the distance the mouse moved
            PictureBox1.Width += movement.X

            ' Next time we will measure from the now-current mouse position.
            DragOrigin = e.Location
        Else
            ' Is mouse within right six-or-so pixels?
            AtRightEdge = e.X > (PictureBox1.Width - DragMarginWidth)

            ' Set the cursor accordingly
            If (AtRightEdge) Then
                PictureBox1.Cursor = Cursors.SizeWE
            Else
                PictureBox1.Cursor = Cursors.Default
            End If
        End If
    End Sub
All we need now is to stop the dragging operation when the user lets go of the mouse.
Code:
    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        ' Stop dragging
        DraggingHorizontal = False
    End Sub
One other thing we'll do to avoid funky behavior is make sure the dragging operation is stopped and the cursor is reset in the MouseLeave event. This will catch odd scenarios where the MouseUp event might not fire, like if another application steals focus while the user is dragging.
Code:
    Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        DraggingHorizontal = False
        PictureBox1.Cursor = Cursors.Default
    End Sub
Not too terribly complicated, right? It shouldn't be too tricky to add vertical dragging in there too (and maybe two-way dragging if the user drags the corner of the picture, yes?). There are a couple of other things you might want to take care of, like making sure the user drags with the left mouse button, and having a minimum size for the image so the user doesn't accidentally make it disappear.
__________________
C# _VB.NET _
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime Resize Controls at runtime
Resize Controls at runtime
Resize Controls at runtime
 
Resize Controls at runtime
Resize Controls at runtime
 
-->