C# 中的弹出消息

Muhammad Maisam Abbas 2024年2月16日
C# 中的弹出消息

本教程将讨论在 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# 弹出消息

在上面的代码中,我们创建了一个弹出消息窗口,每当用户使用 C# 中的 MessageBox.Show() 函数单击 button1 时,该窗口就会显示。

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

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

相关文章 - Csharp GUI