简体   繁体   English

在C#中创建弹出式表单

[英]Creating a Pop-Up form in C#

I am trying to create a windows form that gets displayed for 2 seconds when triggerd by an event, and then closes automatically. 我正在尝试创建一个窗口窗体,在事件触发时显示2秒钟,然后自动关闭。

I have tried several options. 我尝试了几种选择。 This is my current code: 这是我目前的代码:

        this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
        this.aletPopup.Show();
        Thread.Sleep(2000);
        this.aletPopup.Close();

This preforms the actions that I desire, however, when the form loads it does not load the label or image which is on the form. 这样可以执行我希望执行的操作,但是,在加载表单时,它不会加载表单上的标签或图像。 Instead, the area where the image and label are become transparent. 相反,图像和标签变得透明的区域。 My desired output is this: 我想要的输出是这样的: 在此输入图像描述

I have also tried using this.aletPopup.ShowDialog(); 我也尝试过使用this.aletPopup.ShowDialog(); , which does display the graphics. ,它确实显示图形。 However, the form will not close automatically when using this method. 但是,使用此方法时,表单不会自动关闭。

EDIT: I am attempting to use Michael Perrenoud's solution. 编辑:我正在尝试使用迈克尔·佩伦努德的解决方案。 However, I cannot get the form to close. 但是,我无法让表格结束。 I have a timer set at a 2000ms interval which is initally disabled. 我有一个以2000ms为间隔设置的计时器,该计时器最初被禁用。 Am I overriding the OnShown correctly? 我是否正确覆盖了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(); 
    }

Instead, how about leveraging ShowDialog , and then using a Timer on the dialog form. 相反,如何利用ShowDialog ,然后在对话框窗体上使用Timer In the Tick event of the Timer , close the form. TimerTick事件中,关闭表单。

this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
this.aletPopup.ShowDialog();

You could even pass the interval into the .ctor of the dialog form if you wanted some flexibility. 如果需要灵活性,甚至可以将间隔传递到对话框表单的.ctor中。

It's very important to note here that you'd need to leverage the OnShown override to actually Start the Timer so the form is in fact shown to the user. 在这里需要特别注意的是,您需要利用OnShown覆盖来实际Start Timer以便实际上向用户显示该表单。

The reason can be in Message Loop. 原因可能是在消息循环中。 When you block your thread by Thread.Sleep, it also blocks Message loop. 当您通过Thread.Sleep阻塞线程时,它也会阻止Message循环。

You can make like this: 您可以这样:

this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
this.aletPopup.Show();
for(var i = 0; i<= 200; i++)
{
    Thread.Sleep(10);
    Application.DoEvents();
}
this.aletPopup.Close();

DoEvents will process messages from message queue during that time. DoEvents将在这段时间内处理来自消息队列的消息。

When calling Thread.Sleep you're blocking the UI thread, thus preventing it from processing UI events. 调用Thread.Sleep您将阻止UI线程,从而阻止它处理UI事件。

You need to ensure that Close is called after 2 seconds without actually blocking the main thread. 您需要确保2秒钟后调用Close而不实际阻塞主线程。 There are a number of ways of doing this, such as using a Timer , or something like Task.Delay : 有很多方法可以做到这一点,例如使用TimerTask.Delay类的Task.Delay

aletPopup.StartPosition = FormStartPosition.CenterScreen;
aletPopup.Show();
Task.Delay(TimeSpan.FromSeconds(2))
    .ContinueWith(t => aletPopup.Close(), 
    TaskScheduler.FromCurrentSynchronizationContext());

The reason this is happening, is that you are halting the thread that draws the form. 发生这种情况的原因是,您正在暂停绘制表单的线程。 So the form has time to display, but as it's being drawn, the thread is being stopped. 因此,窗体有时间显示,但是在绘制时,线程正在停止。

Easy enough to fix.... 很容易修复....

Add an event handler to the popup for the Load event with the following handler: 使用以下处理程序为Load事件的弹出窗口添加事件处理程序:

private async void handleLoad(Object sender, EventArgs args)
{
  await Task.Delay(2000);
  Close();
}

Remark Because you used Show() , the user could always click around this popup. 备注因为使用了Show() ,所以用户始终可以在此弹出窗口附近单击。 If this is undesirable, then use ShowDialog() instead. 如果这是不合需要的,那么请改用ShowDialog()

Did you try a refresh to redraw the form? 您是否尝试过刷新以重新绘制表格?

    this.aletPopup.StartPosition = FormStartPosition.CenterScreen;
    this.aletPopup.Show();

    this.alertPopup.Refresh();


    Thread.Sleep(2000);
    this.aletPopup.Close();

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

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