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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1806247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:00:21  来源:igfitidea点击:

How to make a simple popup box in Visual C#?

c#visual-studiouser-interfacepopup

提问by neuromancer

When I click a button, I want a box to popup on the screen and display a simple message. Nothing fancy really. How would I do that?

当我单击一个按钮时,我希望在屏幕上弹出一个框并显示一条简单的消息。真的没什么好看的。我该怎么做?

采纳答案by Alex J

System.Windows.Forms.MessageBox.Show("My message here");

Make sure the System.Windows.Formsassembly is referenced your project.

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

回答by AFD

回答by Spence

Just type mboxthen hit tab it will give you a magic shortcut to pump up a message box.

只需输入mbox然后点击选项卡,它就会为您提供一个神奇的快捷方式来打开一个消息框。

回答by user6436606

Try this:

尝试这个:

string text = "My text that I want to display";
MessageBox.Show(text);

回答by shubz

In Visual Studio 2015 (community edition), System.Windows.Formsis not available and hence we can't use MessageBox.Show("text").

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

Use this Instead:

改用这个:

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

Note: Your function must be defined async to use above await Msg.ShowAsync().

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

回答by RooiWillie

Why not make use of a tooltip?

为什么不使用工具提示?

private void ShowToolTip(object sender, string message)
{
  new ToolTip().Show(message, this, Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y, 1000);
}

The code above will show message for 1000 milliseconds (1 second) where you clicked.

上面的代码将在您单击的位置显示 1000 毫秒(1 秒)的消息。

To call it, you can use the following in your button click event:

要调用它,您可以在按钮单击事件中使用以下内容:

ShowToolTip("Hello World");