How to make windows forms remember its state

M

Mario

Hi,

I want to create a windows application with various windows forms. It
is currently designed as a single document interface. What I want to
do is from form1 open form2 (this could be done with a button click or
file menu item).
After I finish doing what I need in form2, I would like to load form1
in the same condition it was prior going to form2. What are your
suggestions? Thanks.
 
A

Arunkumar Keserla

This can be achieved by storing the X and Y coordinates either in the registry
or some configuration file. Choice is yours.

-Arun
 
P

Peter Duniho

Mario said:
I want to create a windows application with various windows forms. It
is currently designed as a single document interface. What I want to
do is from form1 open form2 (this could be done with a button click or
file menu item).
After I finish doing what I need in form2, I would like to load form1
in the same condition it was prior going to form2. What are your
suggestions? Thanks.

The easiest solution would be, instead of closing the form, just hide
it. Then when you show it again, it's the same instance as before and
already is of course exactly as before.

You can prevent the form from closing by overriding OnFormClosing and
setting the FormClosingEventArgs.Cancel property to "true" and hiding
the form before you return. Or, you can simply disable any of the
default closing mechanisms and provide a different UI mechanism (custom
buttom, menu item, whatever) to hide the form and switch to the other one.

Barring that, you'll have to save all of the properties you want to
preserve and then reinitialize a new form later with those properties.
This would be required if you wanted, for example, to preserve the form
state across executions of the application.

Pete
 
A

alphatommy_at_hotmail_dot_com

The easiest solution would be, instead of closing the form, just hide
it. Then when you show it again, it's the same instance as before and
already is of course exactly as before.

You can prevent the form from closing by overriding OnFormClosing and
setting the FormClosingEventArgs.Cancel property to "true" and hiding
the form before you return. Or, you can simply disable any of the
default closing mechanisms and provide a different UI mechanism (custom
buttom, menu item, whatever) to hide the form and switch to the other one.

Barring that, you'll have to save all of the properties you want to
preserve and then reinitialize a new form later with those properties.
This would be required if you wanted, for example, to preserve the form
state across executions of the application.

Pete

Thanks for the prompt feedback. Pete, I want to do what you are
suggesting. However, I can not figure out how to load form1 without
creating a new instance of form1. With a new instance, all previous
conditions are reset.
 
P

Peter Duniho

alphatommy_at_hotmail_dot_com said:
Thanks for the prompt feedback. Pete, I want to do what you are
suggesting. However, I can not figure out how to load form1 without
creating a new instance of form1. With a new instance, all previous
conditions are reset.

You need to retain a reference to the form somewhere, so that you can
reuse the original form instance. There are a variety of ways to do
this, but making the form a singleton may be the cleanest technique for you:

public class Form1 : Form
{
static Form1 _instance;

public static Form1 Instance
{
get
{
if (_instance == null)
{
_instance = new Form1();
}

return _instance;
}
}

private Form1()
{
InitializeComponent();
/* other initialization, etc. */
}
}

You will need to change your Program class so that it uses
"Form1.Instance" rather than "new Form1()" in the Application.Run()
statement. (See a recent thread for alternatives to this:
http://groups.google.com/group/micr...csharp/browse_thread/thread/9e92d24414f6bfe5/)

Then, elsewhere when you want the instance, you also use
"Form1.Instance" instead of constructing a new one yourself.

Pete
 
A

alphatommy_at_hotmail_dot_com

You need to retain a reference to the form somewhere, so that you can
reuse the original form instance. There are a variety of ways to do
this, but making the form a singleton may be the cleanest technique for you:

public class Form1 : Form
{
static Form1 _instance;

public static Form1 Instance
{
get
{
if (_instance == null)
{
_instance = new Form1();
}

return _instance;
}
}

private Form1()
{
InitializeComponent();
/* other initialization, etc. */
}
}

You will need to change your Program class so that it uses
"Form1.Instance" rather than "new Form1()" in the Application.Run()
statement. (See a recent thread for alternatives to this:http://groups.google.com/group/microsoft.public.dotnet.languages.csha...)

Then, elsewhere when you want the instance, you also use
"Form1.Instance" instead of constructing a new one yourself.

Pete

Thanks, Pete. It works great.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top