Swap order within another control (VB.NET, Winform, Panel)

  • Thread starter Thread starter Larkinos
  • Start date Start date
L

Larkinos

Guest
Hi everyone.


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.



'Unidad Origen
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

'Unidad Destino
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


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.

Continue reading...
 
Back
Top