?
Solved

c# 2010 winforms tabs - change visible state

Posted on 2013-02-05
12
Medium Priority
?
692 Views
Last Modified: 2013-02-05
Hi

Is it possible to change the visible state of a tab in a winforms application?

I have some tabs on a form that, for user friendliness, would be better if they were not displayed until a certain 'state' has been reached.

so if I have a tabcontrol with 10 tabs, is it possible to change the visible state 5,6 & 7 until I need them to be available?

Cheers

CD
0
Comment
Question by:cycledude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38854816
you mean hide them?
0
 

Author Comment

by:cycledude
ID: 38854850
hide and make visible too...
0
 

Author Comment

by:cycledude
ID: 38854937
or enable/disable...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38854971
0
 

Author Comment

by:cycledude
ID: 38855099
Hi emoreau

Thanks for the link... I had a look, will that work with c#2010?

I have found another solution, which is simpler (but does not hide tabs, just prevents access to them) and is good enough for my purposes:



 private void tbTabs_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (lblID.Text == "")
            {
                if (e.TabPage == tpDocumentation)
                    e.Cancel = true;
            }
        }

Open in new window


I am using a hidden label on the page which has an id in it when the user has reached a point that I want the tabs to be enabled, so when the user clicks a tab, we check the lblID for a value, if it has no value then we check which tab is clicked, if the tab is one I want to prevent access to, then cancel displaying it.

like I say, it doesn't hide the tab, or even make it look 'disabled' but it prevents access and that is objective no. 1.


Thanks
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38855164
.net code is almost the same in VB and C#, yes it will work.

if you want to hide the tab, just remove the page from the collection
0
 

Author Comment

by:cycledude
ID: 38855377
turns out that tabpages do have an enabled property (not in intellisense though)

you can enable and disable the tabs you want to by:

tabPage1.Enabled = true;

or

tabPage1.Enabled = false;

setting to false disables all the controls on the tab... but does not change the formatting of the 'tab' text ... but still, it works
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 38855410
you can create a tabs manager which caches the tab pages and hide/show them by request:

 public class TabsManager
    {
        Dictionary<TabPage, TabState> _pages = new Dictionary<TabPage, TabState>();
        TabControl _tabCtrl;
        public enum TabState { Hide, Show }

        public TabsManager(TabControl tabCtrl)
        {
            _tabCtrl = tabCtrl;
            int index = 0;
            var pages = tabCtrl.TabPages.Cast<TabPage>();
            foreach (var page in pages)
            {
                page.Tag = index++;
                _pages.Add(page, TabState.Show);
            }
        }

        public void SetState(string name, TabState state)
        {
            var tab = _pages.Keys.Cast<TabPage>().Where(n => n.Text == name).FirstOrDefault();
            if (tab != null)
            {
                SetState(tab, state);
            }
        }

        public void SetState(int index, TabState state)
        {
            var tab = _pages.Keys.Cast<TabPage>().Where(n => (int)n.Tag == index).FirstOrDefault();
            if (tab != null)
            {
                SetState(tab, state);
            }
        }

        private void SetState(TabPage page, TabState state)
        {
            _pages[page] = state;

            switch (state)
            {
                case TabState.Hide:
                    _tabCtrl.TabPages.Remove(page);
                    break;
                case TabState.Show:
                    if (!_tabCtrl.TabPages.Contains(page))
                    {
                        if (_tabCtrl.TabPages.Count > (int)page.Tag)
                        {
                            _tabCtrl.TabPages.Insert((int)page.Tag, page);
                        }
                        else
                        {
                            _tabCtrl.TabPages.Add(page);
                        }
                    }
                    break;
            }
        }
    }

Open in new window


in your form, add TabsManager as member:
 
public partial class Form1 : Form
    {
        TabsManager _tabsManager;

        public Form1()
        {
            InitializeComponent();
            _tabsManager = new TabsManager(this.tabControl1);
        }

Open in new window


now, u can hide/show a tab page by its name or index

_tabsManager.SetState(index, TabsManager.TabState.Hide);
_tabsManager.SetState("tabPage1", TabsManager.TabState.Hide);

Open in new window


the tabs manager stores the order of the pages as well, so when u hide a page and show it again, it will add it to its original position in the tab control.
0
 

Author Comment

by:cycledude
ID: 38855530
hi sedgewick

looks promising... I have some errors though...

Error	1	The type or namespace name 'TabPage' could not be found (are you missing a using directive or an assembly reference?)	c:\path_to_projects\tabs_test\tabs_test\TabsManager.cs	10	20	tabs_test

Error	2	The type or namespace name 'TabControl' could not be found (are you missing a using directive or an assembly reference?)	c:\path_to_projects\tabs_test\tabs_test\TabsManager.cs	11	9	tabs_test

Open in new window


there's a couple more referencing the same items
0
 

Author Comment

by:cycledude
ID: 38855545
got it... missing

using System.Windows.Forms;

from the top
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38855547
add:
using System.Windows.Forms;

Open in new window

0
 

Author Closing Comment

by:cycledude
ID: 38855582
thats fantastic, many thanks for the code!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question