move dynamic Contorls during runtime within the panel or some container like panel-Collection of common programming errors


  • msdn I was trying to move some dynamic control within the parent container like panel or a group box how do i do that?any help
  • 13 Answers


  • msdn1

    Private dragging As Boolean 
        Private beginX, beginY As Integer 
     
        Public Sub Hand_CustomDown(ByVal sender As Object, ByVal e As  _ 
                            System.Windows.Forms.MouseEventArgs) 
     
            dragging = True 
            beginX = e.X 
            beginY = e.Y 
     
        End Sub 
     
        Public Sub Hand_CustomMove(ByVal sender As Object, ByVal e As  _ 
        System.Windows.Forms.MouseEventArgs) 
            If dragging = True Then 
                If e.X > 0 And e.Y > 0 Then 
    ‘ the code for checking the parent container goes here
                    sender.Location = New Point(sender.Location.X + e.X – beginX, _ 
                                       sender.Location.Y + e.Y – beginY) 
                    Me.Refresh() 
     
                End If 
     
            End If 
        End Sub 
     
        Public Sub Hand_CustomUp(ByVal sender As Object, ByVal e As  _ 
        System.Windows.Forms.MouseEventArgs) 
     
            dragging = False 
     
        End Sub 
        

  • msdn2

    any controls made on runtime


  • msdn3

    Add handlers for the MouseDown and MouseMove events and move than as design time controls.


  • msdn4

    the move is the part im looking for and trying to control the move within a container


  • msdn5

    Private Sub Form1_Load() Handles Me.Load 
    Dim T as New Panel 
    T.backColor = Color.Black 
    Me.Controls.Add(T) 
    Addhandler T.MouseDown, Addressof Hand_CustomDown 
    Addhandler T.MouseMove, Addressof Hand_CustomMove 
    Addhandler T.MouseUP, Addressof Hand_CustomUp 
    End Sub 
     
    Dim ShouldIMove as Boolean = False 
     
    Private Sub Hand_CustomDown 
    ShouldIMove = True 
    End Sub 
     
    Private Sub Hand_CustomMove(ByVal Sender as System.Object, ByVal E as System.EventArgs) 
    If ShouldIMove = True Then 
    Sender.Location = Me.PointToClient(Cursor.Position) 
    End If 
    End Sub 
     
    Private Sub Hand_CustomUp 
    ShouldIMove = False 
    End Sub 


  • msdn6 here is my scenarioi  have like panels and each panels have few controls that are created on runtimei would like to move the contorls inside the panel , but cannot be move outside of the panels.

  • msdn7 then just check the location and make sure it is inside the panel:

    Private Sub Hand_CustomMove(ByVal Sender as System.Object, ByVal E as System.EventArgs)   
      
    ‘Check if the Mouse is Down  
    If ShouldIMove = True Then   
        Dim MousePT as Point = Me.PointToClient(Cursor.Position)   
          
        ‘Check if the Mouse Location is Greater than the Upper Left Point’s X  
        ‘Check if the Mouse Location is Less than the Upper Left Point’s Y  
        If (MousePT.X > Panel1.Location.X) And (MousePT.Y  Me.PointToClient(ctrl.Parent.Location).X) And (MousePT.Y > Me.PointToClient(ctrl.Parent.Location).Y) Then 
     
                    ‘Check if the Mouse Location is Less than the Lower Right Point’s X + 10px Buffer   
                    ‘Check if the Mouse Location is Greater than the Lower Right Point’s Y + 10px Buffer   
                    If (MousePT.X