【问题标题】:How to make a simple popup box in Visual C#?如何在 Visual C# 中制作一个简单的弹出框?
【发布时间】:2010-12-20 20:17:48
【问题描述】:

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

【问题讨论】:

    标签: c# visual-studio user-interface popup


    【解决方案1】:
    System.Windows.Forms.MessageBox.Show("My message here");
    

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

    【讨论】:

    • 别忘了分号 :)
    • 要在 VS 中添加对程序集的引用,请右键单击您的项目并单击 Add Reference...。然后可以搜索System.Windows.Forms
    • Assemblies 然后Framework 然后搜索参考。不是来自 COM 选项卡。
    【解决方案2】:

    只需输入mbox,然后点击Tab,它会给你一个神奇的快捷方式来打开一个消息框。

    【讨论】:

    • 注意:这只适用于 Visual Studio。
    • 12 年前我写这篇评论的时候,只有 Visual Studio 的家族;)
    【解决方案3】:

    试试这个:

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

    【讨论】:

      【解决方案4】:

      在 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() 以上使用。

      【讨论】:

      • 我猜你是在 UWP 或 WinRT 下检查。 System.Windows.Forms 应该仍然可用,我很确定。
      【解决方案5】:

      没什么好看的?试试消息框

      http://www.homeandlearn.co.uk/csharp/csharp_s1p9.html

      【讨论】:

        【解决方案6】:

        为什么不使用工具提示?

        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");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-29
          • 1970-01-01
          相关资源
          最近更新 更多