Pooh
Pooh

Reputation: 17

Want to run first form only once after installtion

I have my first form which is taking path of folder and saving that value in user properties and opening the second form. I want to run first form only first time after installation and save that value and don't want to run that form after that. After that I want only second form to run.

This is the code of my first form.

private void button1_Click(object sender, EventArgs e)
{
    folderBrowserDialog1.ShowDialog();
    string Source = folderBrowserDialog1.SelectedPath.ToString();
    Properties.Settings.Default.path = Source;
    Properties.Settings.Default.Save();
    Form1 f = new Form1();
    f.Show();
}

Upvotes: 0

Views: 96

Answers (4)

Pieter
Pieter

Reputation: 460

I tried this n it worked..

       static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //Properties.Settings.Default.Reset();
       if(Properties.Settings.Default.path == "" )

        {
            Application.Run(new FolderSetting());
        }
        else
        {
            Application.Run(new Form1());
        }

    }
}

Thanks.

Upvotes: 1

Brijesh Kumar Tripathi
Brijesh Kumar Tripathi

Reputation: 2976

Write the code in program.cs instead of button click. You should determine if the value of the path is blank only then open folder browser dialog to select the path otherwise directly run your second form. Your code should be like below:

if (Properties.Settings.Default.path.trim() == "")
{
    folderBrowserDialog1.ShowDialog();
    string Source = folderBrowserDialog1.SelectedPath.ToString();
    Properties.Settings.Default.path = Source;
    Properties.Settings.Default.Save();
}
Form1 f = new Form1();
f.Show();

Hope it will solve your problem.

Upvotes: 0

karthickj25
karthickj25

Reputation: 1197

You can use the registry entry for your app to make sure first time form is shown only once. On app start create the key if not exists. Set the key value on the first time form is opened. On each button click open the other form based on the key value set.

By setting the key in registry, you can also give configuration in the app to reset the value if user wish to do so.

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software",true);

key.CreateSubKey("YourAppName");
key = key.OpenSubKey("YourAppName", true);


key.CreateSubKey("FirstTimeFlag");
key = key.OpenSubKey("FirstTimeFlag", true);

// set the key first time
key.SetValue("FirstTimeFlag", "true");

// get Value of key (probably assign this to App Constant)  
var isFirstTimeLogin = (bool)key.GetValue("FirstTimeFlag");

Upvotes: 0

pasha goroshko
pasha goroshko

Reputation: 199

  if(string.IsNullOrWhiteSpace(Properties.Settings.Default.path))      
  {
     folderBrowserDialog1.ShowDialog();
     string Source = folderBrowserDialog1.SelectedPath.ToString();
     Properties.Settings.Default.path = Source;
     Properties.Settings.Default.Save();
  }

  Form1 f = new Form1();
  f.Show();

Upvotes: 1

Related Questions