Click to See Complete Forum and Search --> : Saving Tab Control State on application exit
Rich...
March 26th, 2007, 03:34 PM
Hi Guys, Im trying to output the text property of all my open tabs when the application exits.
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
tabMainTabs.TabPages.Count
to
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.
petes1234
March 26th, 2007, 04:10 PM
In what event is this being called?
Rich...
March 26th, 2007, 04:14 PM
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
Rich...
March 27th, 2007, 02:44 PM
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.
petes1234
March 27th, 2007, 02:59 PM
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.
Rich...
March 28th, 2007, 08:17 AM
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.
petes1234
March 28th, 2007, 08:59 AM
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):
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!
petes1234
March 29th, 2007, 03:39 PM
I see that you marked this as "resolved". How did you resolve the issue? Thanks /Pete
Rich...
April 1st, 2007, 01:34 PM
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.
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...
petes1234
April 1st, 2007, 01:56 PM
Rather than selecting the tabpages, could you do instead:
String strOpenTabs = "";
foreach (TabPage tp in tabMainTabs.TabPages)
{
if (tp.Text != "Welcome")
{
strOpenTabs += tp.Text + ", ";
}
}
Rich...
April 1st, 2007, 02:30 PM
thanks pete,
ill try that now, never knew you could apply the foreach command with tab pages and a tab control.
anyway
Thanks again.
Rich...
April 1st, 2007, 02:32 PM
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...
petes1234
April 1st, 2007, 02:45 PM
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 (http://msdn2.microsoft.com/en-us/library/system.windows.forms.tabcontrol.tabpagecollection(VS.80).aspx) that this class implements the IList, ICollection, IEnumerable interfaces. It is the IEnumerable interface that tells you that foreach will work here.
petes1234
April 1st, 2007, 02:50 PM
Also, you don't really even need to use "foreach". A simple for loop would work just as well:
String strOpenTabs = "";
for (int i = 0; i < tabMainTabs.TabPages.Count; i++)
{
TabPage tp = tabMainTabs.TabPages[i];
if (tp.Text != "Welcome")
{
strOpenTabs += tp.Text + ", ";
}
}
cjard
April 2nd, 2007, 05:53 AM
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..
Rich...
April 6th, 2007, 06:56 PM
Hi Cjards,
you have multiple open tabs in the tab control but one active one as you have said, the purpose was the save the state of the application on exit.
So if the user has 5 open tabs and closes the application, When the application is next loaded, I want those same tabs to load with it.
One further slightly unrelated issue,
Can you use the foreach loop with a combobox?
Ive been trying but cant get it to work
foreach(Item? in combobox.Items)
{
Do Stuff
}
Thanks
Rich...
crackersixx
April 6th, 2007, 07:17 PM
For Each itm As Object In ComboBox1.Items
MessageBox.Show(itm.ToString())
Next
petes1234
April 6th, 2007, 09:08 PM
For Each itm As Object In ComboBox1.Items
MessageBox.Show(itm.ToString())
Next
I think that this is visual basic for .net.
The c# format is something like this: string fooStr = "";
foreach (object myItem in comboBox1.Items)
{
fooStr += myItem.ToString() + Environment.NewLine;
}
MessageBox.Show(fooStr);
If you need to use the items in a more specific way, you will need to cast them first:((myObject).myItem).myObjectMethod();
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.