温馨提示×

如何在C# WinForms中使用Alert

c#
小樊
99
2024-09-08 02:14:08
栏目: 编程语言
Windows服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C# WinForms中,没有内置的“Alert”功能,但是可以使用MessageBox类来创建一个模拟“Alert”的对话框

using System;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 显示一个 Alert 对话框
            ShowAlert("这是一个 Alert 消息!");
        }

        public void ShowAlert(string message)
        {
            MessageBox.Show(message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

在这个例子中,我们创建了一个名为ShowAlert的方法,该方法接受一个字符串参数message。然后,我们使用MessageBox.Show()方法显示一个包含传入消息的对话框。这个对话框有一个“OK”按钮和一个信息图标。当用户点击“OK”按钮时,对话框会关闭。

你可以根据需要调整MessageBoxButtonsMessageBoxIcon枚举来自定义对话框的外观和行为。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:C#中Alert的样式和主题如何设置

0