C # Reordering / rendering multiple layers of panels
I am working on an interesting project to create an image editor, I would like the user to be able to create multiple layers of panels to create the desired materials. I've successfully implemented basic adding and removing layers, but I'm a little confident that it's best to set each panel to "z height" and then display the layers in a specific order.
From what I've learned on the internet, I have to use Controls.setchildindex
panels to set each z-order. All panels are stored in a list, so when I want to display the layers I can just loop over the list.
I skimmed with help Windows.forms.control.canvas
, but this is stitched together to complicate other parts of the program, such as setting a "custom" background color. Anyone got any other ideas on layered panels, a way to use the Controls.setchildindex
best way forward?
Many thanks.
source to share
so after a bit of trial and error I got it working in my layer system. Turns out this is easier than me, but by simply removing all canvas layer elements from the "container panel" (rootPanel), then re-adding them to their panel's specific layer order according to that order. I've included a method below that just removes and re-adds the layers I use Dictionary<String,Panel> panelList
to store panels, I hope someone finds my answer helpful.
private void reOrderLayers(object sender, EventArgs e)
{
for (int i = 0; i < rootPanel.Controls.Count; i++)
{
rootPanel.Controls.RemoveAt(i);
rootPanel.Refresh();
}
for (int i = 0; i < listBox.Items.Count; i++)
{
rootPanel.Controls.Add(panelList[listBox.Items[i].ToString()]);
rootPanel.Refresh();
}
}
source to share