C# 中的彈出訊息
Muhammad Maisam Abbas
2024年2月16日

本教程將討論在 C# 中顯示彈出訊息視窗的方法。
在 C# 中使用 MessageBox
類顯示彈出訊息
如果我們有一個按鈕,並且想在單擊按鈕時顯示彈出訊息,則可以使用 C# 中的 MessageBox
類。MessageBox.Show()
方法在螢幕上以 C# 顯示訊息視窗。MessageBox.Show()
方法將字串格式的訊息作為輸入引數,並將其顯示給使用者。下面的程式碼示例向我們展示瞭如何建立一個簡單的彈出訊息視窗,該視窗顯示一些訊息,以響應使用 C# 中的 MessageBox.Show()
方法單擊按鈕。
using System;
using System.Windows.Forms;
namespace popup {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
string text = "This is some Text that I wanted to show";
MessageBox.Show(text);
}
}
}
輸出:
在上面的程式碼中,我們建立了一個彈出訊息視窗,每當使用者使用 C# 中的 MessageBox.Show()
函式單擊 button1
時,該視窗就會顯示。
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn