如何在Visual C#中创建一个简单的弹出框?

neu*_*cer 43 c# user-interface popup visual-studio

当我点击一个按钮时,我想在屏幕上弹出一个框并显示一条简单的信息.没什么好看的.我该怎么办?

Ale*_*x J 78

System.Windows.Forms.MessageBox.Show("My message here");
Run Code Online (Sandbox Code Playgroud)

确保System.Windows.Forms程序集引用了您的项目.

  • 要在VS中添加对程序集的引用,请右键单击项目,然后单击"添加引用...".然后你可以搜索`System.Windows.Forms`. (9认同)
  • 别忘了分号:) (2认同)

Spe*_*nce 39

只需键入mbox然后点击选项卡它将为您提供一个神奇的快捷方式来打开一个消息框.

  • 花花公子.非常好! (2认同)

shu*_*ubz 5

在 Visual Studio 2015(社区版)中,System.Windows.Forms不可用,因此我们不能使用MessageBox.Show("text").

使用这个代替:

var Msg = new MessageDialog("Some String here", "Title of Message Box");    
await Msg.ShowAsync();
Run Code Online (Sandbox Code Playgroud)

注意:您的函数必须定义为异步才能在上面使用await Msg.ShowAsync()

  • 我猜您正在 UWP 或 WinRT 下进行检查。我相当确定,“System.Windows.Forms”应该仍然可用。 (2认同)