Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi team.

I am trying to move a panel type object (panel child) that is inside another panel type object (Parent panel A) to another panel (Parent panel B), these panels were created dynamically.
I know that in this case it is necessary to obtain the sender object of the parents panels and even there everything well, when I move the child object within its parent panel, it is done without problem, but when I try to move it between parent panels, it does not perform any action . Can you help me in showing me where my mistake is? I attached the generated code.

Thank you in advance and effort.

What I have tried:

This is to get (cut)

Private Sub ToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem3.Click

    clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)

p1Name = clickedPanel.Name

    paParent = clickedPanel.parent

End Sub



This is to do (paste)
Private Sub ToolStripMenuItem4_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem4.Click
    clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)

    p2Name = clickedPanel.Name

    pbParent = clickedPanel.parent

    Call Swap()

End Sub


This is to do the swap.

Private Sub Swap()

        '' build the panel names
        Dim varA As String = p1Name.ToString
        Dim varC As String = p2Name.ToString

        Dim PanelParent As Panel = paParent
        Dim PanelParent_b As Panel = pbParent

        Dim p3 = DirectCast(PanelParent.Controls.Find(p1Name, True).FirstOrDefault(), Panel)
        If p3 Is Nothing Then Throw New ArgumentException("index1")

        Dim p4 = DirectCast(PanelParent_b.Controls.Find(p2Name, True).FirstOrDefault(), Panel)
        If p4 Is Nothing Then Throw New ArgumentException("index2")

        Dim positionA As Integer = Panel1.AutoScrollPosition.X

        Panel1.AutoScroll = False

        Dim temp = p4.Location
        p4.Location = p3.Location
        p3.Location = temp

        Panel1.AutoScroll = True

        Panel1.AutoScrollPosition = New Point(positionA, Panel1.VerticalScroll.Maximum)

    End Sub


Try to do this too.
p3.Location = New Point(p3.Location.X, p3.Location.Y)
PanelParent.Controls.Add(p3)
p4.Location = New Point(p4.Location.X, p4.Location.Y)
PanelParent_b.Controls.Add(p4)
Posted
Updated 19-Feb-20 8:14am

1 solution

Good day.

I found the solution, attach the code.

Thank you all for the effort and time.

Regards.


Private Sub Swap()

    Dim PanelParent As Panel = paParent
    Dim PanelParent_b As Panel = pbParent

    Dim p3 = DirectCast(PanelParent.Controls.Find(p1Name, True).FirstOrDefault(), Panel)
    If p3 Is Nothing Then Throw New ArgumentException("index1")

    Dim p4 = DirectCast(PanelParent_b.Controls.Find(p2Name, True).FirstOrDefault(), Panel)
    If p4 Is Nothing Then Throw New ArgumentException("index2")

    Dim positionA As Integer = Panel1.AutoScrollPosition.X

    Panel1.AutoScroll = False

    Dim temp = p4.Location
    Dim temp_b = p3.Location

    With p3
        .Parent = PanelParent_b
        .Location = temp
    End With

    With p4
        .Parent = PanelParent
        .Location = temp_b
    End With

    Panel1.AutoScroll = True

    Panel1.AutoScrollPosition = New Point(positionA, Panel1.VerticalScroll.Maximum)

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900