如何在C# WinForms中使用Alert

   2024-09-30 8800
核心提示:在C# WinForms中,没有内置的“Alert”功能,但是可以使用MessageBox类来创建一个模拟“Alert”的对话框using System;using Syst

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

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号