【发布时间】:2010-12-20 20:17:48
【问题描述】:
当我单击一个按钮时,我希望在屏幕上弹出一个框并显示一条简单的消息。真的没有什么花哨的。我该怎么做?
【问题讨论】:
标签: c# visual-studio user-interface popup
当我单击一个按钮时,我希望在屏幕上弹出一个框并显示一条简单的消息。真的没有什么花哨的。我该怎么做?
【问题讨论】:
标签: c# visual-studio user-interface popup
System.Windows.Forms.MessageBox.Show("My message here");
确保您的项目引用了 System.Windows.Forms 程序集。
【讨论】:
Add Reference...。然后可以搜索System.Windows.Forms。
Assemblies 然后Framework 然后搜索参考。不是来自 COM 选项卡。
只需输入mbox,然后点击Tab,它会给你一个神奇的快捷方式来打开一个消息框。
【讨论】:
试试这个:
string text = "My text that I want to display";
MessageBox.Show(text);
【讨论】:
在 Visual Studio 2015(社区版)中,System.Windows.Forms 不可用,因此我们无法使用 MessageBox.Show("text")。
改用这个:
var Msg = new MessageDialog("Some String here", "Title of Message Box");
await Msg.ShowAsync();
注意:您的函数必须定义为 async 才能在 await Msg.ShowAsync() 以上使用。
【讨论】:
System.Windows.Forms 应该仍然可用,我很确定。
【讨论】:
为什么不使用工具提示?
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);
}
上面的代码将在您单击的位置显示 1000 毫秒(1 秒)的消息。
要调用它,您可以在按钮单击事件中使用以下内容:
ShowToolTip("Hello World");
【讨论】: