0

How would I run a different form whenever this program is run? for example:

On the first time the program is ran, run new Form1().Show();

On the second time the program is ran, run new Form2().Show(); for example.

In my case I am running on startup so

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)

i want it so Form1 is ran on the first time you open the program and whenever it runs on startup Form3 is opened

conjure
  • 55
  • 7
  • 2
    you need to save the form you want to run in a file in machine and during program start read that from the file and run it – Mostafiz May 01 '16 at 16:20
  • @MostafizurRahman im thinking of creating a file like formran.txt and set the text to "True" when the program is ran on the first time but i have no idea how to do that – conjure May 01 '16 at 16:29
  • 1
    Basic file operations if that's the route you're choosing to go with: https://msdn.microsoft.com/en-us/library/system.io.file(v=vs.110).aspx – B.K. May 01 '16 at 16:31
  • http://stackoverflow.com/questions/20819112/show-dialog-on-first-start-of-application – ilansch May 01 '16 at 17:16
  • Make the project a console project. Then call the two forms from the console project. – jdweng May 01 '16 at 18:36

2 Answers2

1

What you are actually asking for is how to save some value (eg. Int programOpensCounter or Boolean isThisProgramRunningForFirstTime) which will be stored after program was closed.

The proper way to do this is use Settings.settings file:

Here's short tutorial how to do this. If you would like to learn more here are articles from Microsoft

So saving form name would look like this:

Properties.Settings.Default["StoredFormName"] = "Form2";    
Properties.Settings.Default.Save();

Opening form which name was stored as string in Settings would look like this:

string formName = Properties.Settings.Default.StoredFormName;
Type CAType = Type.GetType("FormNamespaceHere." + formName);
var obj = Activator.CreateInstance(CAType);
Form theFormYouStored = (Form)obj;
theFormYouStored.Show();
Community
  • 1
  • 1
szooky
  • 177
  • 1
  • 12
0

Suppose you want to save the form name which run in next time in a text file name form.txt locate in C: drive then you can read write text file like below

// if next time will run form2. 
string createText = "form2";
File.WriteAllText(@"c:\form.txt", createText);

// Open the file to read form name. 
string readText = File.ReadAllText(@"c:\form.txt");

using this you can save next running form name during program exit or when do you want. And during run read form name from file and run that form like below in your Program.cs file

 [STAThread]
 static void Main()
 {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);

      // Open the file to read form name. 
      string readText = File.ReadAllText(@"c:\form.txt");
      if(readText == "form1")
      {
         Application.Run(new Form1());
      }
      else if(readText == "form2")
      {
         Application.Run(new Form2());
      }
 }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42