Can anyone tell how can i modify my object at run time?
I have a form containing two panels; textbox, picturebox, and a label are in the panel1 where as panel2 is likely to be the user screen.. User drag the textbox from panel1 to panel2 and it is added there.. Now, i want to provide the facility to the user to modify the size using mouse similarly as we use to modify using mouse while making our forms.. It is like re sizing the button or picturebox at run time.

Member Avatar

Add the MouseMove event to the handles clause of the Control_MouseMove Sub for any controls you want to edit.

You should be able to figure out what its doing.

Option Strict On

Public Class Form1

    <Flags()> _
    Private Enum EditAction
        Drag = 1
        West = 2
        East = 4
        North = 8
        South = 16
    End Enum

    Private Sub Control_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

        Dim SenderAsControl As Control = CType(sender, Control)
        Dim PaddingBuffer As Integer = 6
        Dim PaddingBufferPlus As Integer = PaddingBuffer + 1

        Select Case e.Button

            Case Windows.Forms.MouseButtons.None
                'No button is held
                'Change the cursor and set the edit action
                'so when the button is held you can tell
                'what to do.
                Select Case e.X
                    Case Is <= PaddingBuffer
                        'Left Edge of Control
                        'Either North + West, West, or South + West
                        Select Case e.Y
                            Case Is <= PaddingBuffer
                                SenderAsControl.Cursor = Cursors.SizeNWSE
                                SenderAsControl.Tag = EditAction.North Or EditAction.West

                            Case PaddingBufferPlus To SenderAsControl.Height - PaddingBufferPlus
                                SenderAsControl.Cursor = Cursors.SizeWE
                                SenderAsControl.Tag = EditAction.West

                            Case Else
                                SenderAsControl.Cursor = Cursors.SizeNESW
                                SenderAsControl.Tag = EditAction.South Or EditAction.West
                        End Select

                    Case PaddingBufferPlus To SenderAsControl.Width - PaddingBufferPlus
                        'Middle of control
                        'Either North, drag, or South
                        Select Case e.Y
                            Case Is <= PaddingBuffer
                                SenderAsControl.Cursor = Cursors.SizeNS
                                SenderAsControl.Tag = EditAction.North

                            Case PaddingBufferPlus To SenderAsControl.Height - PaddingBufferPlus
                                'In the middle.  Allow move.
                                SenderAsControl.Cursor = Cursors.SizeAll
                                SenderAsControl.Tag = EditAction.Drag
                                SenderAsControl.Cursor.Tag = e.Location
                            Case Else
                                SenderAsControl.Cursor = Cursors.SizeNS
                                SenderAsControl.Tag = EditAction.South

                        End Select

                    Case Is >= SenderAsControl.Width - PaddingBufferPlus
                        'Right edge of control
                        'Either: North + East, East, South + East
                        Select Case e.Y
                            Case Is <= PaddingBuffer
                                SenderAsControl.Cursor = Cursors.SizeNESW
                                SenderAsControl.Tag = EditAction.North Or EditAction.East
                            Case PaddingBufferPlus To SenderAsControl.Height - PaddingBufferPlus
                                SenderAsControl.Cursor = Cursors.SizeWE
                                SenderAsControl.Tag = EditAction.East
                            Case Else
                                SenderAsControl.Cursor = Cursors.SizeNWSE
                                SenderAsControl.Tag = EditAction.South Or EditAction.East
                        End Select
                    Case Else
                        SenderAsControl.Cursor = Cursors.Arrow
                        SenderAsControl.Tag = EditAction.none
                End Select

            Case Windows.Forms.MouseButtons.Left
                'Left mouse button is held.
                'Extract the action out of the tag
                'and do the appropriate shit.
                If TypeOf SenderAsControl.Tag Is EditAction Then
                    Dim TheCurrentAction As EditAction = CType(SenderAsControl.Tag, EditAction)

                    If TheCurrentAction = EditAction.Drag Then
                        'The old location is stored in the cursors tag.
                        'Extract it and move the button
                        If TypeOf SenderAsControl.Cursor.Tag Is Point Then
                            Dim LastLocation As Point = CType(SenderAsControl.Cursor.Tag, Point)
                            Dim NewLocation As Point
                            NewLocation.X = SenderAsControl.Location.X + (e.Location.X - LastLocation.X)
                            NewLocation.Y = SenderAsControl.Location.Y + (e.Location.Y - LastLocation.Y)
                            SenderAsControl.Location = NewLocation
                        End If

                    End If

                    If (TheCurrentAction And EditAction.East) = EditAction.East AndAlso e.X > 0 Then
                        SenderAsControl.Width = e.X
                    End If

                    If (TheCurrentAction And EditAction.South) = EditAction.South AndAlso e.Y > 0 Then
                        SenderAsControl.Height = e.Y
                    End If

                    If (TheCurrentAction And EditAction.West) = EditAction.West Then
                        Dim CurrentRight As Integer = SenderAsControl.Right
                        SenderAsControl.Left += e.X
                        SenderAsControl.Width = CurrentRight - SenderAsControl.Left
                    End If

                    If (TheCurrentAction And EditAction.North) = EditAction.North AndAlso e.Y < SenderAsControl.Height Then
                        Dim CurrentBottom As Integer = SenderAsControl.Bottom
                        SenderAsControl.Top += e.Y
                        SenderAsControl.Height = CurrentBottom - SenderAsControl.Top
                    End If

                End If

        End Select

    End Sub

End Class

Don't forget to mark your threads as solved.

commented: Nice solution, as always +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.