Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Good day all!!!

I have a tabcontrol that I'm dynamically adding tabpages to. I wanted to find out how do I close the tabpage from within itself. I'm hosting a control inside the tabpage that has the menu option to close the tabpage. How would I go about doing this.

Thanx in advance...
Posted
Comments
Maciej Los 20-Aug-13 18:29pm    
What have you tried till now? Where are you stuck?
WinForms? WebControls?
Sergey Alexandrovich Kryukov 20-Aug-13 18:38pm    
I do understand the confusion. It is related to the fact that the event handler is associated with some control to be removed. I tried to comfort OP with my answer, please see...
—SA
Maciej Los 20-Aug-13 18:52pm    
But i do not understand the OP's confusion ;(
azinyama wrote: I'm hosting a control inside the tabpage that has the menu option to close the tabpage. - That's a Clue, that's a Key!
I'm not sure, but i feel deep in my soul that OP is looking for SelectedTab property of TabControl...
Sergey Alexandrovich Kryukov 20-Aug-13 19:02pm    
Well, selected tab, or selected index of a tab... Such a menu inside a tag is a bit unusual, so OP could be confused by his own bravery (something tries to delete itself) :-), but there is nothing wrong in it. :-)
—SA
Sergey Alexandrovich Kryukov 20-Aug-13 18:41pm    
You should always tag the UI library or application type you are using when UI is involved.
When you say "TabPage", the question would be: which one? Full type name, please.
—SA

1 solution

There is no such concept as "close" for a tab page. You can delete one, here is how:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.tabpages.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.tabpagecollection.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.tabpagecollection.remove.aspx[^] or:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.tabpagecollection.removeat.aspx[^].

Note, that there is no such thing as "from within itself" or not. Typically, you invoke some events, and the event handler is an anonymous method or a method of, typically, a form or a window. For example:
C#
myDeleteButton.Click += (sender, eventArgs) => { // or menu item click, same thing...
    myTabControl.TabPages.RemoveAt(myTabControl.SelectedIndex);
    // if myDeleteButton is placed on some page, and you can click it, it means that the page is selected before the click
    // this button itself will be removed with the page, as the result of this action,
    // so what?
};


The references above are for System.Windows.Forms, but the code for WPF (and probably some other libraries) will be very similar.

Note: you should always tag the UI library or application type you are using when UI is involved.

—SA
 
Share this answer
 
v6
Comments
Maciej Los 20-Aug-13 18:53pm    
As always, very good answer!
+5!
Sergey Alexandrovich Kryukov 20-Aug-13 18:58pm    
Thank you, Maciej.
—SA
Ron Beyer 20-Aug-13 20:03pm    
Also +5'd, I was always disappointed that the TabCollection or Tab Page didn't have a method for marking a tab as not visible, seems like that would be trivial from the framework standpoint...
Sergey Alexandrovich Kryukov 20-Aug-13 20:17pm    
Agree, this is a missing feature.
Thank you, Ron.
—SA
Maciej Los 21-Aug-13 1:46am    
Agree with both of you ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900