Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retaining the last state of a form when you re-open it

Status
Not open for further replies.

nishasp

Programmer
May 13, 2004
33
0
0
GB
Does anyone know how i can open up a form that keeps the last state of its controls as they were before the form was closed eg buttons are disabled when the form is closed, but when the form loads, the buttons are enabled, but need them to be disabled???
 
if you change the visibility of the form (not close it) then the state is maintained. implement the onClose event to stop the form from closing (which means destroying it - and on open it actually creates it) and make it just hide. the same when you open it (instead of creating it just make it visible - but make sure you create the form once, when your application starts)

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Try creating a registry tree for your app, and include form states. To learn more on how to use the registry, look for the Registry class under the Microsoft.Win32 namespace
 
nishasp,
Or... simply use a small delimited text file (you can use XML as well) that stores your form settings and store it in a folder within your application's directory - that's what I do. That way, you don't have to deal with the registry and you can restore form settings even after the whole application closes.

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
Hi JC,

How would i go about writing to a delimited text file with the settings stored? I would appreciate if you could show me some sample code.

Thanks,

nisha
 
nisha,
I apologize for my late response.

For what you need, I think it would be better to use XML as opposed to delimited text files, but I'm not well-versed in XML and thus, I can't provide an example for that.

A simple delimited text file, however, will do what you need. I use it the following way:

First I designate how many lines the file will have. Each line will represent the settings for a different item. Thus, if you want to reload the settings for 10 items, your file (based on my method) will have 10 lines. Next, I choose a delimiter to separate the different settings within each line. Say, for instance, that you want to keep the following settings for a checkbox control: Left, Top, Width, Height, and Checked. If you used the semicolon to delimit these settings, then the line in your file that stores these settings might look like this:

[tt]0;0;50;50;true[/tt]

That line says that the Left and Height properties are both 0, that the Height and Width are both 50, and that the Checked property is true.

To write this info to the text file, you would do something like this:
Code:
FileInfo fi = new FileInfo("settings.txt");
StreamWriter sw = fi.CreateText();
sw.WriteLine("0;0;50;50;true");    
sw.Write(sw.NewLine);
sw.close;
To get the data in the file, you would do something like this:
Code:
StreamReader sr = File.OpenText("settings.txt");
string[] settings;
string input = null;
int line = 0;
[green]// Read each line in the file[/green]
while ((input = sr.ReadLine()) != null)
{
  line++;
  
  settings = input.Split(';');
  
  [green]// Do something different based on line[/green]
  if (line == 1)
  {    
    chkBox.Left = int.Parse(settings[0]);
    chkBox.Top = int.Parse(settings[1]);
    chkBox.Height = int.Parse(settings[2]);
    chkBox.Width = int.Parse(settings[3]);
    if (settings[4] == "true")
      chkBox.Checked = true;
    else
      chkBox.Checked = false;  
  }
  ...
}
sr.close;
I hope the above code made some sense. You are in full control of how the data in the file is organized. The sample text file I provided contained only one line but yours will have many, I assume, and you can use a switch statement (and constant values) to determine what to do for every line. Oh and one last thing: Whenever data is written to the settings.txt file, the old data is overwritten and you must provide all settings each time you save.

This method has worked for me and I use it all the time to save the user preferences and settings. I create a folder within my application for named after the user logged on to the app and save/restore settings from that folder.

Hope this helps!

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
I will use the Serialize method.
First of all, mark the objects of the form for the serialization:
Code:
[Serializable]
public class MyForm 
{
  public int iConnection;
  [NonSerialized] public Button btnLogin;
  public string strConnection;
}

Next use the BinaryFormatter class to save the state:

MyForm frm = new MyForm();
Stream fs = File.Open("frm.bin", FileMode.Create, FileAccess.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, frm);
fs.Close();

// Load 
Stream fr = File.Open("frm.bin", FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
MyForm frm2 = (MyForm) bf.Deserialize(fr);
fr.Close();
// Before displaying the the frm object, load the saved state file to know everything abount the object.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top