Solved

c# 2010 winforms tabs - change visible state

Posted on 2013-02-05
12
610 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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