Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm looking for a way to add controls to the controlCollection at a specific index.

There's a RemoveAt(index) method but no AddAt(index) method. Why is that and how can I achieve this functionality.

The only way I can think of is to remove every control beyond the index and reinsert them afterwards but that's pretty stupid.

Debug me...
  • Administrators
Posted

IIRC you cannot choose where to add a control into the collection, is there a particular reason the control needs to be inserted in a specific place rather than added to the end?

If you wish to be able to access them in a particular order from the GUI then set the control's tabindex property - that should do the trick.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
IIRC you cannot choose where to add a control into the collection' date=' is there a particular reason the control needs to be inserted in a specific place rather than added to the end?[/quote']

 

I'm using a panel to hold a variable number of items (different types of controls). These items can be resized, removed and added. These operations have sideeffects on the other items. Even on other items on different panels.

That's why each item has it's own index property resembling it's position in the panel's controlCollection.

Since there's a lot of stuff happening at the same time I need to identify the items very fast (through their index in the controlCollection).

I know I could loop through the controlCollection compare instances and check their own index property but that's a lot slower than accessing them through their index.

E.g. : I have ten items and want to delete the fifth. The sixth to tenth items will have to change their location now. Right now I can loop through the controlCollection from the sixth to tenth element directly. If they're not in order I would have to loop through the whole collection and check each item's index property.

To keep the order I will have to place items correctly when adding new ones.

 

If you wish to be able to access them in a particular order from the GUI then set the control's tabindex property - that should do the trick.

 

The tabindex isn't helping here.

 

If there's no other way I will have to do it this way:

If I want to add an element next to the fifth I remove (and copy) all following controls add the new one at the sixth position and reinsert the old ones again.

 

It's not very nice but I have to keep order in the controlCollection.

Debug me...
Posted
this.Controls.SetChildIndex(ctrl' date='newIndex);[/quote']

 

thx that's it. Shame on me but I didn't see it though I was looking at MSDN :o

 

 

Just curious why does the ControlsCollection not have the insert method like an arraylist? That would do the same but require only one method to be called.

Debug me...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...