Click here to Skip to main content
11,920,772 members (60,875 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Forms
I have 4 different forms. It is obvious that I can change the form which will be started when I run the application on the following line:

Application.Run(new Form1()); // I do not want to change this line maunally.

I want to change "new Form1" part by coding from other forms as well.

For example:

First, I run the application and Form1 starts. I click on the button to open Form2. When I open Form2, Application.Run(new Form1()) should be changed to Application.Run(new Form2()). So, when I close the whole application and start again, Form2 will start. In this case, I will be able to save my latest open Form.

How can I change the Application.Run(new Form1()) programmatically ?

Thanks in advance...
Posted 1-May-13 9:04am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Don't. Instead, have a MasterForm which is always opened from Application.Run. It's Visible property (and show in taskbar property) is set to false.
It looks in the application config file, and decides which form to show (lets say Form1)
It displays form1, and hooks the FormClosed Event to close itself.
When Form1 presses for form2, it signals a "Form2Please" event which the MasterForm handles. It closes Form1, displays Form2, and changes the application config file for next time.
"Well, it seems ok, but how to write on App.Config file from code side, then ? it writes itself ?"

Yep, pretty much.
1) Open your projects Properties in the solution explorer, and double click on "Settings.settings"
2) In the resulting grid, change the Name to "MySetting", and set the Value to "Defaulted value". Leave Type and Scope as "string" and "User" respectively.
3) Save and close the settings window.
4) To read your setting:
string s = Properties.Settings.Default.MySetting;
5) To write your setting:
Properties.Settings.Default.MySetting = "My new setting value";
Sam-et 1-May-13 14:35pm
Well, it seems ok, but how to write on App.Config file from code side, then ? it writes itself ?
OriginalGriff 1-May-13 14:44pm
Answer updated
Kenneth Haugland 1-May-13 14:47pm
Added a quote block, couldnt resist :-)
OriginalGriff 1-May-13 14:50pm
Feel free! :laugh:
Sam-et 1-May-13 14:53pm
I got this. Thank you. My last question would be where to set/write my "new Form1,2,3" ?
OriginalGriff 1-May-13 15:02pm
In the MasterForm, just before you display it:
Form2 f2 = new Form2();
f2.FormClosing += new FormClosingEventHandler(f2_FormClosing);
f2.Form1Please += new EventHandler(f2_Form1Please);
f2.Form3Please += new EventHandler(f2_Form3Please);

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

  Print Answers RSS
0 OriginalGriff 548
1 Dave Kreskowiak 245
2 phil.o 170
3 KrunalRohit 134
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,815
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100