[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.
Re: Saving Tab Control State on application exit
In what event is this being called?
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
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.
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.
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.
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!
Re: Saving Tab Control State on application exit
I see that you marked this as "resolved". How did you resolve the issue? Thanks /Pete
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...
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 + ", ";
}
}
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.
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...
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.
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 + ", ";
}
}
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..