温馨提示×

如何在C# WinForms中使用Alert

c#
小樊
81
2024-09-08 02:14:08
栏目: 编程语言

在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枚举来自定义对话框的外观和行为。

0