Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I simply want to save a TabControl from System.Windows.Forms in C# with all its tabs (tab key, tab text, tab tooltiptext, tab tag, tab imageindex) included in a file and then later on load it again from a file. Found nothing on this. The tabcontrol has also no ".save" extension function so far I know.

What I have tried:

My new approach:

C#
void Button1Click(object sender, EventArgs e)
{
  foreach(TabPage Tab1 in tabControl1.TabPages)
  {
  string TabData="";			     
TabData+=Tab1.ToolTipText+","+Tab1.Text+","+Tab1.Name+","+Tab1.ImageIndex+","+Tab1.Tag+"\r\n";
  }
  File.CreateText(TabData); //Here comes the exception: Invalid FileName!
}     


How do I store the tabs in the file, which would be the right framework command for it? "File.Save" does not exist, so I chose "File.CreateText", but it would not do. How would I read back the info afterwards? "File.Load" does not exists.
Posted
Updated 21-Apr-16 23:34pm
v5
Comments
FranzBe 19-Apr-16 8:38am    
you need to serialize the content of your control before trying to save.
you may want to have a look here:

http://www.codeproject.com/Articles/86503/Saving-the-state-serializing-a-Windows-Form
Jochen Arndt 19-Apr-16 8:56am    
Not related to your question but worth a note:

You don't want to use loops for speed reasons but are using shell commands to open a notepad session and then pass data using interactions?

Why not just create a text file and write to it?

I don't think that there is a function providing the required data. So you have to iterate through your tabs and get the information yourself (an existing function would do the same just hiding the loop).

I still don't know why you don't like loops. It is impossible to write programs without loops (besides a simple "Hello World"). Most API functions will use a lot of loops internally.
Richard MacCutchan 19-Apr-16 10:06am    
Short answer, which others have tried to explain is: you don't. You cannot just save some Window including all the labels, textboxes, controls and their content. You need to use serialization, which will loop through each control on the page (Tab, Form, Control etc), and save information about them and their content, that you can later deserialize back to the original.

What you have done in your code above will never work.

1 solution

First off, no there is no command - "saving" controls of any sort is not a normal behaviour.
And second: the "no loops" parts of this is going to make your life difficult - because the only reason for saving something is because the content is going to change - and part of the tab control content is it's TabPages collection, each of which has a nested Controls collection which "makes it what it is". Unless you save each TabPage and it's controls then you can't restore the TabControl.

Start start by thinking about what you are trying to achieve, and work out just what you need to save: I do something similar by having a User class which is DB stored, and each TabPage in my TabControl contains a single UserControl which accepts a User and displays their information. That way, I create the tabpage for each user, and fill in the usercontrol for it:
C#
List<User> justUsers = users.Values.OrderByDescending(u => entries[u].Max(r => r.CreditLimit)).ToList();
foreach (User user in justUsers)
    {
    tp = new TabPage(user.LongName);
    tp.DoubleClick += UserTabPage_DoubleClick;
    tp.Tag = user;
    UserDataView view = new UserDataView(user, entries[user]);
    view.Dock = DockStyle.Fill;
    view.ToggleDisplay += view_ToggleDisplay;
    view.TargetValue = Properties.Settings.Default.TargetValue;
    view.TargetChanged += view_TargetChanged;
    tp.Controls.Add(view);
    tabUsers.TabPages.Add(tp);
    }
 
Share this answer
 
Comments
OriginalGriff 19-Apr-16 9:55am    
I don't "send the tabs to notepad" - that's the whole idea. And "notepad" is an application, not a file storage system...
OriginalGriff 19-Apr-16 9:57am    
Well...unless you just copied my code and assumed it would work in your application without any changes - which given that I can't see your code would be a bit...um...naive - there isn't a lot I can say, is there?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900