C# Alert弹出框自动关闭实例
时间: 2023-07-12 18:16:12 浏览: 155
以下是一个 C# Alert 弹出框自动关闭的示例:
```csharp
using System;
using System.Windows.Forms;
using System.Threading;
public class Program
{
static void Main()
{
// 创建一个 Alert 弹出框
var alert = new Alert("这是一个自动关闭的弹出框", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// 启动一个新线程,等待 3 秒后关闭弹出框
new Thread(() =>
{
Thread.Sleep(3000);
alert.Invoke(new Action(() => alert.Close()));
}).Start();
// 显示 Alert 弹出框
alert.ShowDialog();
}
}
public class Alert : Form
{
public Alert(string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
Text = caption;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
Icon = SystemIcons.Information;
ControlBox = false;
var label = new Label
{
Text = message,
AutoSize = true,
Location = new System.Drawing.Point(10, 10)
};
Controls.Add(label);
var okButton = new Button
{
DialogResult = DialogResult.OK,
Text = "确定",
Location = new System.Drawing.Point(140, 60)
};
Controls.Add(okButton);
AcceptButton = okButton;
DialogResult = DialogResult.OK;
}
}
```
这个示例创建了一个 Alert 弹出框,并在启动一个新线程后等待 3 秒后自动关闭弹出框。你可以根据需要修改线程等待时间。
阅读全文
相关推荐


















