且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

首次启动应用程序时显示对话框

更新时间:2022-10-29 16:17:21

您可以将其保存为设置中的布尔,您应该检查第一个表单的加载事件。
您的设置文件应该有一个设置,我称之为FirstRun,执行以下步骤:


  1. 右键单击您的项目

  2. 点击属性

  3. 点击设置标签页(可能在左边)

  4. 添加设置我做过如上图所示

注意: Scope 可以更改应用程序,如果这是您的应用程序的需要,因为您没有在您的问题中提及。



您的设置文件应如下图所示:





public void Form1_Load(object sender,EventArgs e)
{
if((bool)Properties.Settings.Default [FirstRun] == true)
{
//第一个应用程序运行
//更新设置
Properties.Settings.Default [FirstRun] = false;
//保存设置
Properties.Settings.Default.Save();
//创建要显示的对话框的新实例
FirstDialogForm fdf = new FirstDialogForm();
//显示对话框
fdf.ShowDialog();
}
else
{
//不是运行应用程序的第一次。
}
}

注意:从我的手机写下来,所以我无法编译测试

编辑:从桌面检查代码并添加图像。


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?

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.