溫馨提示×

如何在C# WinForms中使用Alert

c#
小樊
123
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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女