SetChildIndex

What can cause SetChildIndex to not reorder a control? I have a case where the following assertion fails:



Controls.SetChildIndex(z, index);
Debug.Assert(Controls[index] == z);


I couldn't find anything that might suggest SetChildIndex fires any events and checked that Control.OnControlAdded/Control.OnControlRemoved are not being fired.

Thanks,
Ray

[668 byte] By [RayManning] at [2007-12-16]
# 1

Can you post some code that reproduces that?

DavidM.Kean at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2


Actually, I can no longer reproduce it. I put the code back to the way it was and did not hit the assertion. Here's the code I have/had for what it's worth:

#if !BUGBUG
Debug.Assert(Controls.Count > 2);
Controls.Add(z);
Debug.Assert(z == Controls[Controls.Count-1]);
z.Controls.Add(Controls[index]);

Controls.SetChildIndex(z, index); // put it back where it was
Debug.Assert(Controls[index] == z);

#else
Debug.Assert(Controls.Count > 2);
Control control = Controls[index];
z.Controls.Add(control); // reparent the control (and removes the control)
Controls.Add(z); // add the zone back in the control's place.
Debug.Assert(Controls[Controls.Count - 1] == z);

Controls.SetChildIndex(z, index); // put it back where it was
Debug.Assert(Controls[index] == z);
#endif

The first case was failing while the second case worked. I have munged other code though so the problem was quite possibly a side effect of something else. Does SetChildIndex trigger any events?

Thanks,
Ray

RayManning at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
I am having a similar problem when trying to move around converted controls
<code>
int x = 2;
c1 = Controls[x]
Controls.RemoveAt(x);
// change control to check box
ConvertToCheckBox(ref c1);
// push last added back on
Controls.Add(c1);
Controls.SetChildIndex(c1, x);
Controls[1] == c1;
</code>

it seems that when x = 1, c1 is placed at 1
when x = 2, c1 is placed at 1
when x = 3, c1 is placed at 3

is anybody else confused?

changerOfSea at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...