简体   繁体   English

WinForm C#:检查首次运行并显示消息

[英]WinForm c#: Check first run and show message

I am creating a winform application containing first run check. 我正在创建一个包含首次运行检查的Winform应用程序。 I've been following these 2 articles: 我一直在关注这两篇文章:

First run check is supposed to check if application has ever been run and if not, it should show some message to the user. 首次运行检查应该检查应用程序是否曾经运行过,如果没有运行过,它应该向用户显示一些消息。 Problem i am having is, that this message is displayed before winform application is initialized/displayed and I am not able to find out why. 我遇到的问题是,此消息是 Winform应用程序初始化/显示之前显示的,我无法查明原因。 Here is my code: 这是我的代码:

Program.cs Program.cs

public static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

Form1.cs Form1.cs

public Form1()
{
    this.InitializeComponent();
    CheckFirstRun();
}

private static void CheckFirstRun()
{
    if(Settings.Default.FirstRun)
    {
        MessageBox.Show(
            "First run");
        Settings.Default.FirstRun = false;
        Settings.Default.Save();
}

It shows Message box with msg: "First run" and after clicking OK button it shows WinForm. 它显示带有msg的消息框:“首次运行”,然后单击“确定”按钮后,它显示WinForm。 What I am trying to achieve is to Display WinForm first and if it is first run then show this msgBox. 我想要实现的是先显示WinForm,如果先运行,则显示此msgBox。

Any ideas? 有任何想法吗?

Instead of calling CheckFirstRun() from constructor you can call it Form.Shown 而不是调用的CheckFirstRun()从构造函数可以调用它Form.Shown

Form.Shown Event 表单显示事件

The Shown event is only raised the first time a form is displayed; 仅在第一次显示表单时才引发Shown事件。 subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event 随后最小化,最大化,还原,隐藏,显示或无效化和重绘不会引发此事件

private void Form1_Shown(Object sender, EventArgs e) 
{
    CheckFirstRun();
}

Overriding OnShown 覆盖OnShown

The OnShown method also allows derived classes to handle the event without attaching a delegate. OnShown方法还允许派生类在不附加委托的情况下处理事件。 This is the preferred technique for handling the event in a derived class, MSDN 这是在派生类MSDN中处理事件的首选技术。

Notes to Inheritors When overriding OnShown in a derived class, be sure to call the base class's OnShown method so that registered delegates receive the event, MSDN . 对继承者的说明在派生类中重写OnShown时,请确保调用基类的OnShown方法,以便已注册的委托接收事件MSDN

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    CheckFirstRun();
}

Call CheckFirstRun from OnShown method. OnShown方法调用CheckFirstRun

The Shown event occurs whenever the form is first shown. 每当首次显示表单时,就会发生Shown事件。

[...] [...]

The OnShown method also allows derived classes to handle the event without attaching a delegate. OnShown方法还允许派生类在不附加委托的情况下处理事件。 This is the preferred technique for handling the event in a derived class. 这是在派生类中处理事件的首选技术。

public Form1()
{
    this.InitializeComponent();
}

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    CheckFirstRun();
}

private static void CheckFirstRun()
{
    if(Settings.Default.FirstRun)
    {
        MessageBox.Show(
            "First run");
        Settings.Default.FirstRun = false;
        Settings.Default.Save();
    }
}

Use this event to fire the event after form has load. 加载表单后,使用此事件来触发该事件。

private void Form1_Shown(Object sender, EventArgs e) {

MessageBox.Show("First run.");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM