Malfunction Posted August 13, 2004 Posted August 13, 2004 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. Quote Debug me...
Administrators PlausiblyDamp Posted August 13, 2004 Administrators Posted August 13, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Engine252 Posted August 13, 2004 Posted August 13, 2004 add your control to the collection and then change it's index that should do it Quote
Malfunction Posted August 14, 2004 Author Posted August 14, 2004 add your control to the collection and then change it's index that should do it Which index do you mean? Quote Debug me...
Malfunction Posted August 14, 2004 Author Posted August 14, 2004 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. Quote Debug me...
Engine252 Posted August 14, 2004 Posted August 14, 2004 this.Controls.SetChildIndex(ctrl,newIndex); Quote
Malfunction Posted August 14, 2004 Author Posted August 14, 2004 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. Quote Debug me...
Engine252 Posted August 14, 2004 Posted August 14, 2004 i don't know but you can add it quit simple yourself now right Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.