can we change an index of an object?

  • Thread starter Thread starter Tee
  • Start date Start date
T

Tee

Hi,

Does anyone know how to change an index of an object?

This is what I want to do.

eg: I have 5 controls in a panel.
index from 0 to 4, after I remove the 3rd control (index 2), I want to
change the control with index 3 to 2, and 4 to 3. (push up) ... so when I
add new control to the panel, the new control will have the index 4.

Is this possible? if not, can anyone suggest me a solution?

Thanks,
Tee
 
Tee said:
eg: I have 5 controls in a panel. [...]
I want to change the control with index 3 to 2, and 4 to 3.
(push up) ... so when I add new control to the panel, the
new control will have the index 4.
Is this possible?

I don't think so. However, you can achieve this by removing the
controls from the panel, and then adding them back in a different
order. I had to do this with TabPages on a TabControl when the VS.NET
Forms Designer kept switching them around.

Why do you need to change the indexes?
If you're doing something like this:

for (int i = 0; i < myPanel.Controls.Count; i++)

.... you can rewrite it more clearly like this:

foreach (Control ctl in myPanel.Controls)

P.
 
Property Panel.Controls and TabControl.TabPages are different stories.
Panel.Controls return Control.ControlCollection which has SetChildIndex
Method.
TabControl.TabPages return TabControl.TabPageCollection.

Paul E Collins said:
Tee said:
eg: I have 5 controls in a panel. [...]
I want to change the control with index 3 to 2, and 4 to 3.
(push up) ... so when I add new control to the panel, the
new control will have the index 4.
Is this possible?

I don't think so. However, you can achieve this by removing the
controls from the panel, and then adding them back in a different
order. I had to do this with TabPages on a TabControl when the VS.NET
Forms Designer kept switching them around.

Why do you need to change the indexes?
If you're doing something like this:

for (int i = 0; i < myPanel.Controls.Count; i++)

.... you can rewrite it more clearly like this:

foreach (Control ctl in myPanel.Controls)

P.
 
As I said, I want to make a index ID push up.
so let's say, I removed the control index 3, the 4 will take place the 3
position.
And when I want to remove the new index 3, I just call Controls.RemoveAt(3),
instead of I need to call RemoveAt(4), which makes it don't look right, as
the 4 is already in the index 3 position after 3 got removed.



Paul E Collins said:
Tee said:
eg: I have 5 controls in a panel. [...]
I want to change the control with index 3 to 2, and 4 to 3.
(push up) ... so when I add new control to the panel, the
new control will have the index 4.
Is this possible?

I don't think so. However, you can achieve this by removing the
controls from the panel, and then adding them back in a different
order. I had to do this with TabPages on a TabControl when the VS.NET
Forms Designer kept switching them around.

Why do you need to change the indexes?
If you're doing something like this:

for (int i = 0; i < myPanel.Controls.Count; i++)

... you can rewrite it more clearly like this:

foreach (Control ctl in myPanel.Controls)

P.
 
Back
Top