Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


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

Thanks in advance...
Posted

1 solution

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.
Quote:
"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";
Properties.Settings.Default.Save();
 
Share this answer
 
v3
Comments
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" ?

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