?
Solved

c# 2010 winforms tabs - change visible state

Posted on 2013-02-05
12
Medium Priority
?
739 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:Meir Rivkin
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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:
Meir Rivkin 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:Meir Rivkin
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 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