在C#中创建弹出窗体

Ada*_*ela 2 .net c# winforms

我正在尝试创建一个窗口窗体,在事件触发时显示2秒钟,然后自动关闭.

我尝试了几种选择.这是我目前的代码:

        this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
        this.aletPopup.Show();
        Thread.Sleep(2000);
        this.aletPopup.Close();
Run Code Online (Sandbox Code Playgroud)

这预先形成了我想要的动作,但是,当表单加载时,它不会加载表单上的标签或图像.相反,图像和标签变得透明的区域.我想要的输出是这样的: 在此输入图像描述

我也尝试使用this.aletPopup.ShowDialog();,它显示图形.但是,使用此方法时,表单不会自动关闭.

编辑:我正在尝试使用Michael Perrenoud的解决方案.但是,我无法让表格结束.我有一个设置为2000毫秒间隔的计时器,它被初始禁用.我是否正确地覆盖了OnShown?

public AlertPopForm()
    {
        InitializeComponent();

    }

    private void closingTimer_Tick(object sender, EventArgs e)
    {
        closingTimer.Enabled = false;
        this.Close();
    }

    private void AlertPopForm_OnShown(object sender, System.EventArgs e)
    {
        closingTimer.Enabled = true;
        closingTimer.Start(); 
    }
Run Code Online (Sandbox Code Playgroud)

Mik*_*oud 6

相反,如何利用ShowDialog,然后Timer在对话框表格上使用.在Tick的情况下Timer,关闭窗体.

this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
this.aletPopup.ShowDialog();
Run Code Online (Sandbox Code Playgroud)

.ctor如果您想要一些灵活性,您甚至可以将间隔传递到对话框表单中.

这里需要注意的是,您需要OnShown实际Start使用覆盖,以便实际Timer向用户显示该表单,这一点非常重要.