Here's a minimal demonstration of the problem for anyone who's curious. Would be nice to find out whether this is a Windows Forms bug or a well-known legacy behavior.
using System; using System.Windows.Forms; using System.Drawing;
public class ZForm : Form { Panel thePanel = new Panel(); Button theButton = new Button();
////////////////////// static void Main() { Application.Run(new ZForm(new Size(316, 348))); }
////////////////////// public ZForm( Size s ) { this.ClientSize = s; thePanel.Bounds = new Rectangle(0, 0, 316, 316); this.Controls.Add(thePanel); theButton.Location = new Point(8, 324); this.Controls.Add(theButton); this.theButton.Click += new System.EventHandler(this.theButton_Click); }
////////////////////// void theButton_Click(object sender, System.EventArgs e) { Point tLoc = new Point(16, 196); Size tSize = new Size(128, 128); int ix;
// Ten textboxes and an arbitrary ordering TextBox[] theList = new TextBox[10]; int[] theShowOrder = {2, 3, 7, 6, 9, 1, 0, 5, 8, 4};
// Make a neat stack of invisible textboxes for( ix=0; ix <10; ix++) { tLoc.Offset(16, -16); theList[ix] = makeTextBox(tLoc, tSize, ix); }
// Add the whole stack to the panel, but still invisible thePanel.Controls.Clear(); thePanel.Controls.AddRange(theList);
// Redundant and futile attempt to assure zorder for( ix=0; ix <10; ix++) { thePanel.Controls.SetChildIndex(theList[ix], ix); }
// Show controls in arbitrary order foreach( int jx in theShowOrder) { (theList[jx]).Show(); }
// Re-layer controls to intended zorder -- now it works MessageBox.Show(this, "Before 2nd SetChildIX Loop"); for( ix=0; ix <10; ix++) { thePanel.Controls.SetChildIndex(theList[ix], ix); } }
////////////////////// TextBox makeTextBox( Point p, Size s, int ix ) { TextBox t = new TextBox(); t.Location = p; t.Size = s; t.Text = ix.ToString(); t.Multiline = true; t.Visible = false; return t; } } |