Click here to Skip to main content
Rate this: bad
good
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.
 

So,
How can I change the Application.Run(new Form1()) programmatically ?
 
Thanks in advance...
Posted 1-May-13 9:04am
Sam-et384

1 solution

Rate this: bad
good
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.
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();
  Permalink  
v3
Comments
Sam-et at 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 at 1-May-13 14:44pm
   
Answer updated
Kenneth Haugland at 1-May-13 14:47pm
   
Added a quote block, couldnt resist :-)
OriginalGriff at 1-May-13 14:50pm
   
Feel free! :laugh:
Sam-et at 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 at 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);
f2.Show();

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2014
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