[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

c# 2010 winforms tabs - change visible state

Posted on 2013-02-05
12
Medium Priority
?
721 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: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
Technology Partners: 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:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

649 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