LARC
LARC

Reputation: 109

Swap order within another control

I have a panel that in turn has several panels inside and these are generated dynamically, what I am doing is that by means of the mouse event, I have a menu that allows me to exchange the positions of one panel with another, that is, if I I want to exchange the places of PanelA with that of PanelB, PanelB takes the position of PanelA and PanelA of PanelB, so far no problem.

The detail that I have is to export the information of the collection of the child Panels to an excel sheet, these come out as they were created, I imagine that by its Tabindex and what I want is that if a panel exchange position with another, in this Action also swapped the order as they were created so that in the Ecel report, they appear in the order that it is.

I was seeing the Panel.Controls.GetChildIndex property and it throws me a value, which I imagine is the index with which it is created, is this correct? How can I interchange the order in how they were created within another panel?

I tried the following.

Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
Dim varIndex As Integer = Panel1.Controls.GetChildIndex(clickedPanel)

Is this correct to obtain the index as they were created and later change that index?

If not, how can I make this change?

Thank you in advance.

I tried the following too, but not result.

Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

    Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
    Dim varIndex As Integer = Panel1.Controls.GetChildIndex(clickedPanel)

    p1Name = clickedPanel.Name
    paParent = clickedPanel.Parent
    indexOrigen = clickedPanel.TabIndex

End Sub


Private Sub ToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.Click

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

    p2Name = clickedPanelDestino.Name
    pbParent = clickedPanelDestino.Parent
    indexDestino = clickedPanelDestino.TabIndex

    Dim p1 = DirectCast(Panel1.Controls.Find(p1Name, True).FirstOrDefault(), Panel)
    If p1 Is Nothing Then Throw New ArgumentException("indexOrigen")
    Dim p2 = DirectCast(Panel1.Controls.Find(p2Name, True).FirstOrDefault(), Panel)
    If p2 Is Nothing Then Throw New ArgumentException("indexDestino")

    Dim temp = p2.Location
    Dim tempIndex = indexDestino
    p2.Location = p1.Location
    p2.TabIndex = p1.TabIndex
    p1.Location = temp
    p1.TabIndex = tempIndex
    
End Sub

Upvotes: 1

Views: 38

Answers (1)

LARC
LARC

Reputation: 109

After a while, this is my solution.

'Source Unit (Cut Option)
    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

        Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
        varIndexA = Panel1.Controls.GetChildIndex(clickedPanel)

        p1Name = clickedPanel.Name
        paParent = clickedPanel.Parent
        indexOrigen = clickedPanel.TabIndex

    End Sub

'Target Unit (Paste Option)
    Private Sub ToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.Click

        Dim clickedPanelDestino = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
        varIndexB = Panel1.Controls.GetChildIndex(clickedPanelDestino)
        p2Name = clickedPanelDestino.Name
        pbParent = clickedPanelDestino.Parent

        Dim p1 = DirectCast(Panel1.Controls.Find(p1Name, True).FirstOrDefault(), Panel)
        If p1 Is Nothing Then Throw New ArgumentException("indexOrigen")
        Dim p2 = DirectCast(Panel1.Controls.Find(p2Name, True).FirstOrDefault(), Panel)
        If p2 Is Nothing Then Throw New ArgumentException("indexDestino")

        Dim temp = p2.Location
        p2.Location = p1.Location
        Panel1.Controls.SetChildIndex(p2, varIndexA)
        p1.Location = temp
        Panel1.Controls.SetChildIndex(p1, varIndexB)

    End Sub

Thank you.

Upvotes: 2

Related Questions