How do I Insert a new object in the middle of a Control Collection

M

Mark Denardo

Hi I have a TabControl object that I need to insert new TabPages into. To
append them to the end of the Controls Collection I can do:

tc.controls.add(tp)

(tc - tabcontrol object)
(tp - tabpage object)

But how can I insert them into the middle of collection? I see a 'RemoveAt'
method that takes an index to remove at a specific location, how come
there's not an 'AddAt' or 'Insert' method?

This seems like something that would be quite common for anyone working with
TabControls (or any type of Control Collection for that matter). What am I
missing here?
 
M

Mark Denardo

You'll have to show me an example, because:

tc.controls.insert(3, tp) gives me:

- 'Insert' is not a member of
'System.Windows.Forms.Control.ControlCollection'.

And tc.Insert(3, tp) gives me:

- 'System.Windows.Forms.TabControl.Private Sub Insert(index As Integer,
tabPage As System.Windows.Forms.TabPage)' is not accessible in this context
because it is 'Private'.
 
M

Matt

Mark said:
You'll have to show me an example, because:

tc.controls.insert(3, tp) gives me:

- 'Insert' is not a member of
'System.Windows.Forms.Control.ControlCollection'.

And tc.Insert(3, tp) gives me:

- 'System.Windows.Forms.TabControl.Private Sub Insert(index As Integer,
tabPage As System.Windows.Forms.TabPage)' is not accessible in this context
because it is 'Private'.

Do an "add" followed by a "setchildindex" to move the control to where
you want it. I think this will accomplish what you are trying to do.

Matt
 
E

Erick Sgarbi

Sorry, I failed to see that you need exclusively for Control.Controls.
Yes, as Matt mentioned you'll need to use SetChildIndex or GetChildIndex
for operating control index change on a ControlCollection. The IList
implementation in this object is explicit and throwing
NotSupportedException for Insert (as I can remember...).
ControlCollection depends on doing several internal updates for the
owner control when an index is changed... consequently
SetChildIndex/GetChildIndex are used instead.
 
M

Mark Denardo

Thanks Matt, that solves the Controls Collection part.

tc.Controls.Add(tp)
tc.Controls.SetChildIndex(tp, index)

I got it to properly place the tabpage in the correct location in the
Controls Collection where I wanted it, but...

But I can't seem to get the TabControl to update it to the proper position.
After I perform the first instruction, the TabControl placed it at the end
of my current list of tabpages, but after the second instruction, the
Controls Collection updated property (because I did a foreach on the
collection and verified it was placed in the correct spot), but the tab I
just added stays at the rear of the tabpages. I guess hoping the TabControl
would do this work for me was being a little too optimistic.

Do you (or anyone working with TabControls) know how I can get the
TabControl to update its TabPages based on it's newly changed Controls
Collection. I searched all of the TabControl and TabPages properties, but
can't seem to find anything that does want I want.
 
M

Matt

Mark said:
Thanks Matt, that solves the Controls Collection part.

tc.Controls.Add(tp)
tc.Controls.SetChildIndex(tp, index)

I got it to properly place the tabpage in the correct location in the
Controls Collection where I wanted it, but...

But I can't seem to get the TabControl to update it to the proper position.
After I perform the first instruction, the TabControl placed it at the end
of my current list of tabpages, but after the second instruction, the
Controls Collection updated property (because I did a foreach on the
collection and verified it was placed in the correct spot), but the tab I
just added stays at the rear of the tabpages. I guess hoping the TabControl
would do this work for me was being a little too optimistic.

Do you (or anyone working with TabControls) know how I can get the
TabControl to update its TabPages based on it's newly changed Controls
Collection. I searched all of the TabControl and TabPages properties, but
can't seem to find anything that does want I want.

Okay, I see what you are doing. No, that isn't going to work. Tab
Controls are more or less set in stone when you display them. You can
*try* to add to them, but you are going to get weird behavior at best.

In order to do this, you need to clear ALL the pages, store them in an
array of tab pages, insert the page you want into that array, then put
the pages back into the control.

I know I saw an example of this one codeproject.com at one point... (as
he runs off and looks). Yep. This should do close to what you want:

http://www.codeproject.com/cs/miscctrl/draggabletabcontrol.asp

matt
 
M

Mark Denardo

Yeah that's what I was using as a work around after you showed me the
inserting method, but it seemed hokie and thought there should be a better
way, but I guess clearing and re-adding will be good enough. Ok I
appreciate all of your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top