Show dialog on first start of application

谁说我不能喝 提交于 2019-11-30 04:49:35

问题


Is there an easy way to show an dialog when the program is started for the first time (and only the first time), for some kind of instruction or specifying settings?


回答1:


You could save it as a bool in your settings and you should check at load event of first form. Your settings file should have a setting that I called "FirstRun" do this with following steps:

  1. Right click your Project
  2. Click "Properties"
  3. Click "Settings" tabpage(probably on the left)
  4. Add setting like I did as seen in image above

Note: The Scope can be changed to "Application", if that is your application's need, since you didn't mention in your question.

Your Settings file should look like image below:

public void Form1_Load(object sender, EventArgs e)
{
    if((bool)Properties.Settings.Default["FirstRun"] == true) 
    {
       //First application run
       //Update setting
       Properties.Settings.Default["FirstRun"] = false;
       //Save setting
       Properties.Settings.Default.Save();
       //Create new instance of Dialog you want to show
       FirstDialogForm fdf = new FirstDialogForm();
       //Show the dialog
       fdf.ShowDialog();
    }
    else
    {
       //Not first time of running application.
    }
}

Note: wrote this from my phone, so I couldn't compile to test
Edit: Checked code and added image from desktop.




回答2:


You can have bool value in your settings file which is a "user setting" which means you can change it to true save it for this specific user. When your application starts just check that value. If it's false show your dialog and change it to true and it will stay true.

public void Form_Load(object sender, EventArgs e)
{
    if(Settings.Default.ShowDialog) 
    {
       Settings.Default.ShowDialog = false;
       Settings.Default.Save();
       // show first disalog
    }
    // rest of code if needed
}

Here's an MSDN link on user settings: http://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx




回答3:


Ok, so I assume you're creating WinForms application. First of all, locate the Load event in your main Form event lists (or simply double click your Form in Designer panel). The following method stub will pop up:

public void Form1_Load(object sender, EventArgs e)
{

}

And modify it like this:

public void Form1_Load(object sender, EventArgs e)
{
    MessageBox.Show("Your message here");
}


来源:https://stackoverflow.com/questions/20819112/show-dialog-on-first-start-of-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!