Solved

c# 2010 winforms tabs - change visible state

Posted on 2013-02-05
12
590 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
Comment Utility
you mean hide them?
0
 

Author Comment

by:cycledude
Comment Utility
hide and make visible too...
0
 

Author Comment

by:cycledude
Comment Utility
or enable/disable...
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 

Author Comment

by:cycledude
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
.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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cycledude
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
got it... missing

using System.Windows.Forms;

from the top
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
add:
using System.Windows.Forms;

Open in new window

0
 

Author Closing Comment

by:cycledude
Comment Utility
thats fantastic, many thanks for the code!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now