Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

How to make windows forms  remember its state

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mario - 22 Aug 2007 23:01 GMT
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.
Arunkumar Keserla - 22 Aug 2007 23:48 GMT
This can be achieved by storing the X and Y coordinates either in the registry
or some configuration file. Choice is yours.

-Arun

> Hi,
>
[quoted text clipped - 5 lines]
> in the same condition it was prior going to form2.  What are your
> suggestions?  Thanks.
Peter Duniho - 23 Aug 2007 01:05 GMT
> I want to create a windows application with various windows forms.  It
> is currently designed as a single document interface.  What I want to
[quoted text clipped - 3 lines]
> 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
alphatommy_at_hotmail_dot_com - 23 Aug 2007 01:41 GMT
> > I want to create a windows application with various windows forms.  It
> > is currently designed as a single document interface.  What I want to
[quoted text clipped - 20 lines]
>
> 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.
Peter Duniho - 23 Aug 2007 02:02 GMT
> 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/microsoft.public.dotnet.languages.csharp/browse_t
hread/thread/9e92d24414f6bfe5/
)

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

Pete
alphatommy_at_hotmail_dot_com - 23 Aug 2007 02:36 GMT
> > 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
[quoted text clipped - 37 lines]
>
> Pete

Thanks, Pete.  It works great.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.