CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Feb 2007
    Posts
    46

    [Resolved] Saving Tab Control State on application exit

    Hi Guys, Im trying to output the text property of all my open tabs when the application exits.

    Code:
                
    String strOpenTabs = "";
    
                for (int intTab = 0; intTab < tabMainTabs.TabPages.Count; intTab++)
                {
                    tabMainTabs.SelectedIndex = intTab;
    
                    if (tabMainTabs.SelectedTab.Text != "Welcome")
                    {
                        strOpenTabs += tabMainTabs.SelectedTab.Text + ", ";
    
                    } // End if
    
                } // End for loop
    This code adds all open tabs text value to a string (which is then written out to a file). The problem im having is that Count always equals 0. Ive trade changning

    Code:
    tabMainTabs.TabPages.Count
    to
    Code:
    tabMainTabs.TabCount
    with the same results.

    Im geussing the tab control has already been removed? anyone have any ideas how I can get past this issue?

    Thanks in advance

    Rich

    Resolved: Tab control does not have an event suited to this nor does form closing as far as I was able to find out. I have settled for keeping a record of what tabs are opened and closed through the applications running as writing this out instead of doing it on application end.
    Last edited by Rich...; March 28th, 2007 at 08:28 AM. Reason: resolved

  2. #2
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Saving Tab Control State on application exit

    In what event is this being called?

  3. #3
    Join Date
    Feb 2007
    Posts
    46

    Re: Saving Tab Control State on application exit

    Hi pete,

    This is in a seperate method, the method is however called from the FormClose event. This is of course the form that has the tab control on it.

    Thanks

    Rich

  4. #4
    Join Date
    Feb 2007
    Posts
    46

    Re: Saving Tab Control State on application exit

    It just occured to me that there may be an event for the tab control that would be better suited to this.. Does anyone know of one?

    Thanks

    Rich.

  5. #5
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Saving Tab Control State on application exit

    How about calling it from FormClosing rather than FormClosed. The former is called before the form is closed so its controls may still be active.

  6. #6
    Join Date
    Feb 2007
    Posts
    46

    Re: Saving Tab Control State on application exit

    Hi Pete,

    Form closing has the same result... Im going to try placing it in perhaps Tab Closing (if there is one) or something similar.

  7. #7
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Saving Tab Control State on application exit

    I tried to see what happens on formclosed and formclosing by creating a small app w/ nothing but a tab control and several tab pages. In my little app, I could get the tabpage's text on both events (see below):
    Code:
            private string getTabPagesText()
            {
                string fooStr = "";
                foreach (TabPage tp in tabControl1.TabPages)
                {
                    fooStr += tp.Text + "; ";
                }
                return fooStr;
            }
    
            private void SubForm7_FormClosed(object sender, FormClosedEventArgs e)
            {
                MessageBox.Show("formClosed " + getTabPagesText());
            }
    
            private void SubForm7_FormClosing(object sender, FormClosingEventArgs e)
            {
                MessageBox.Show("formClosing " + getTabPagesText());
            }
    I have a feeling that something else is wrong in your code. Has something else closed the tabs before the forms are closed? are you sure that tabMainTabs is the name for your tabcontrol?

    What I would do would be to try to simplify the problem as much as possible to try to isolate the offending code. Create a small app w/ nothing but the tab control and essential related entities, and then keep adding functionality until it breaks.

    Good luck!

  8. #8
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Saving Tab Control State on application exit

    I see that you marked this as "resolved". How did you resolve the issue? Thanks /Pete

  9. #9
    Join Date
    Feb 2007
    Posts
    46

    Re: Saving Tab Control State on application exit

    Hi Pete,

    I added the resolved entry as an edit to the first post. Ive copied it below.

    Resolved: Tab control does not have an event suited to this nor does form closing as far as I was able to find out. I have settled for keeping a record of what tabs are opened and closed through the applications running as writing this out instead of doing it on application end.

    One problem im not experiencing is however when the code runs through the open tabs to make a note of their names you can see the application change tab.

    Im sure ive seen a solution to this somewhere but cant find it.

    The solution as far as I remember involved making a copy of the tab control and then performing the actions on the copy in memory and then destroying the copy. So the Tab control visible to the user is never changed.

    The code im using currently is below. This code effects what is displayed as it cycles through the open tabs.

    Code:
                    for (int intTab = 0; intTab < tabMainTabs.TabPages.Count; intTab++)
                    {
                        tabMainTabs.SelectedIndex = intTab;
    
                        if (tabMainTabs.SelectedTab.Text != "Welcome")
                        {
                            strOpenTabs += tabMainTabs.SelectedTab.Text + ", ";
    
                        } // End if
    
                    } // End for loop
    This code builds a string (strOpenTabs) containing the names of the open tabs seperated by a comma. This allows me to do further work with the names of the open tabs (Saving and loading etc) using a reg expression (or other string editing) to just look for the commas and know where each name starts and ends.

    So to summarise, does anyone know how to cycle through tabs without actually changing the tab that is displayed to the user?.

    Thanks

    Rich...

  10. #10
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Saving Tab Control State on application exit

    Rather than selecting the tabpages, could you do instead:
    Code:
                
                String strOpenTabs = ""; 
                foreach (TabPage tp in tabMainTabs.TabPages)
                {
                    if (tp.Text != "Welcome")
                    {
                        strOpenTabs += tp.Text + ", ";
    
                    } 
                }

  11. #11
    Join Date
    Feb 2007
    Posts
    46

    Re: Saving Tab Control State on application exit

    thanks pete,

    ill try that now, never knew you could apply the foreach command with tab pages and a tab control.

    anyway

    Thanks again.

  12. #12
    Join Date
    Feb 2007
    Posts
    46

    Re: Saving Tab Control State on application exit

    Hi pete,

    Just pasted your suggest code on top of mine, It works perfectly and doesnt cause that silly flicker through the tabs.

    Thanks a lot for your help.

    Rich...

  13. #13
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Saving Tab Control State on application exit

    Quote Originally Posted by Rich...
    ill try that now, never knew you could apply the foreach command with tab pages and a tab control.

    anyway

    Thanks again.
    Your welcome. TabPages is an instance of the TabPageCollections class. You will find on review of the msdn that this class implements the IList, ICollection, IEnumerable interfaces. It is the IEnumerable interface that tells you that foreach will work here.

  14. #14
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Saving Tab Control State on application exit

    Also, you don't really even need to use "foreach". A simple for loop would work just as well:
    Code:
                String strOpenTabs = "";
                for (int i = 0; i < tabMainTabs.TabPages.Count; i++)
                {
                    TabPage tp = tabMainTabs.TabPages[i];
                    if (tp.Text != "Welcome")
                    {
                        strOpenTabs += tp.Text + ", ";
                    }
                }

  15. #15
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Saving Tab Control State on application exit

    The one thing that confuses me about this discussion: there do you get a TabControl that has more than one open "page" at once?
    The whole idea of a tabcontrol is that there is only one active tab.. In which case, I'd have just told you to bind the SelectedIndex to the settings and save them on exit..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured