Click here to Skip to main content
15,886,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i'm using TabControl on my win form.
Also, i'm able to hide or show tab pages using add and remove method but don't know how to check if tabpage is visible .
Here is example
2 tab pages visible (tabpage1 name = "Page1" and tabpage2 name ="Page2")
When I want to add Page2, first I want to check if Page2 is actually visible . If yes then I would only set focus on the same otherwise I would add the same.

Please if someone can help me how to make this working...

Thanks
Almir
Posted
Comments
[no name] 13-Oct-12 11:20am    
And the SelectedIndex or the SelectedTab properties do not work?

Try:
C#
foreach (TabPage tp in myTabControl.TabPages)
    {
    if (tp.Text == "Page2")
        {
        ...
        break;
        }
    }

Or you could look at the TabPage.Name property, or use the TabControl.TabPages.Contains method if you have the whole TabPage available.



"Thanks
i followed your proposal as follows


C#
if (customTabControl1.TabPages.Contains(Page2))
            {
                customTabControl1.SelectedTab = Page2;
            }
            else
            {
 
                customTabControl1.TabPages.Add(Page2);
                customTabControl1.SelectedTab = Page2;
            } 

but I get the following error when I try to Select added tabpage Page2

cannot access a disposed object

Can you help me to solve the problem.

Thanks"


When you tried to hide Page2 by removing it from the list of tab pages, you didn't keep a reference to it, so the Garbage Collector has Disposed on the page - there isn't a whole lot you can do about that, other than keep a reference (which would be a good idea anyway) when you remove it.

What code are you using to show / hide tab pages?
 
Share this answer
 
v2
Comments
Sandeep Mewara 13-Oct-12 15:58pm    
OP posted a comment as an answer. Had some code so did not copy-pasted here. Once you have a look, you can decide to delete the answer or still keep it.
C#
private void tabacc_Receivable_Selected(object sender, TabControlEventArgs e)
{
    if (e.TabPage == tabacc_Client)
    {
        dgvacc_ClientRefresh();
    }
    if (e.TabPage == tabEditNew)
    {
        LoadRecordInformation();
    }
    if (e.TabPage == tabClientBalance)
    {

    }
}
 
Share this answer
 
Thanks
i followed your proposal as follows

C#
if (customTabControl1.TabPages.Contains(Page2))
            {
                customTabControl1.SelectedTab = Page2;
            }
            else
            {

                customTabControl1.TabPages.Add(Page2);
                customTabControl1.SelectedTab = Page2;
            }


but I get the following error when I try to Select added tabpage Page2

cannot access a disposed object

Can you help me to solve the problem.

Thanks
 
Share this answer
 
Comments
Sandeep Mewara 13-Oct-12 15:57pm    
This is not an answer. Please use 'Have a Question or Comment' link to respond to an answer. It will notify answerer of your comment such that he can come back and reply. Or if needed, use Improve Question link to edit/update your question at anytime.
AlmirM 13-Oct-12 16:25pm    
Thanks
i followed your proposal as follows


Collapse | Copy Code
if (customTabControl1.TabPages.Contains(Page2))
{
customTabControl1.SelectedTab = Page2;
}
else
{

customTabControl1.TabPages.Add(Page2);
customTabControl1.SelectedTab = Page2;
}
but I get the following error when I try to Select added tabpage Page2

cannot access a disposed object

Can you help me to solve the problem.

Thanks
OriginalGriff 14-Oct-12 3:53am    
Please reply via my solution, using the Comment facility - that way I get an email to say you need further help.
When you do next time, I will delete this solution thread to make things tidier (it will eventually remove the effects of the downvotes as well).
AlmirM 14-Oct-12 4:17am    
OK...my bad...It was up to me why this error stat to appears...

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