【问题标题】:How do I create a MessageBox in C#?如何在 C# 中创建 MessageBox?
【发布时间】:2008-09-08 04:00:54
【问题描述】:

我是第一次安装C#,乍一看和VB6很像。我决定从尝试制作“Hello, World!”开始。用户界面版。

我从表单设计器开始,制作了一个名为“Click Me!”的按钮。继续双击它并输入

MessageBox("Hello, World!");

我收到以下错误:

MessageBox 是“类型”,但用作“变量”

很公平,在 C# 中 MessageBox 似乎是一个对象。我尝试了以下

MessageBox a = new MessageBox("Hello, World!");

我收到以下错误: MessageBox 不包含采用“1”参数的构造函数

现在我被难住了。请帮忙。

【问题讨论】:

  • 在学习 C# 时,请牢记其面向对象的基础。与 js 或 php 不同,您的方法调用通常是 method(),在 C# 中,您的调用是 object.method()。例如:Console.WriteLine() 而不是 print()

标签: c# .net


【解决方案1】:

MessageBox.Show 还返回一个 DialogResult,如果您在其中放置一些按钮,则意味着您可以让它返回用户单击的内容。大多数时候我会写类似的东西

if (MessageBox.Show("Do you want to continue?", "Question", MessageBoxButtons.YesNo) == MessageBoxResult.Yes) {
     //some interesting behaviour here
}

我想这有点笨拙,但它可以完成工作。

请参阅https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.dialogresult 了解您可以在此处使用的其他枚举选项。

【讨论】:

    【解决方案2】:

    代码摘要:

    using System.Windows.Forms;
    
    ...
    
    MessageBox.Show( "hello world" );
    

    Also (as per this other stack post):在 Visual Studio 中展开解决方案树中的项目,右键单击引用,添加引用,在框架选项卡上选择 System.Windows.Forms。这将使 MessageBox 与上面使用的 System.Windows.Forms 参考一起工作。

    【讨论】:

      【解决方案3】:

      它是 MessageBox 类的一个静态函数,简单的方法是使用

      MessageBox.Show("my message");
      

      在 System.Windows.Forms 类中。您可以在此 here 的 msdn 页面上找到更多信息。除此之外,您还可以控制消息框文本、标题、默认按钮和图标。由于您没有指定,如果您尝试在网页中执行此操作,您应该查看触发 javascript alert("my message");或确认(“我的问题”);功能。

      【讨论】:

        【解决方案4】:

        试试下面的代码:

        MessageBox.Show("Test Information Message", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);
        

        【讨论】:

        • 请使用:“使用 System.Windows.Forms;”在顶部
        • 2008 年提出并回答了这个问题,并得到了接受的答案。为什么要重复相同的答案。
        • 在给出答案时,请告诉 OP 为什么这有效而他的代码无效。在这种情况下,您使用了静态方法,但他正在尝试创建一个新实例。
        • 我试图定义一个带有消息、标题、消息按钮和图标的消息框。我在上面找不到那个特定的答案,所以我认为我的答案会对用户有所帮助。
        【解决方案5】:

        您也可以使用MessageBoxOKCancel 选项,但它需要很多代码。 if 块用于OKelse 块用于Cancel。代码如下:

        if (MessageBox.Show("Are you sure you want to do this?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
        {
            MessageBox.Show("You pressed OK!");
        }
        else
        {
            MessageBox.Show("You pressed Cancel!");
        }
        

        您还可以将MessageBoxYesNo 选项一起使用:

        if (MessageBox.Show("Are you sure want to doing this?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            MessageBox.Show("You are pressed Yes!");
        }
        else
        {
            MessageBox.Show("You are pressed No!");
        }
        

        【讨论】:

          【解决方案6】:

          System.Windows.Forms 课程中,您可以在MSDN 页面上找到更多信息。除此之外,您还可以控制消息框文本、标题、默认按钮和图标。由于您没有指定,如果您尝试在网页中执行此操作,您应该查看触发 javascript alert("my message");confirm("my question"); 函数。

          【讨论】:

            【解决方案7】:

            这是您可以放入消息框中的一些内容。享受
            MessageBox.Show("Enter the text for the message box",
            "Enter the name of the message box",
            (Enter the button names e.g. MessageBoxButtons.YesNo),
            (Enter the icon e.g. MessageBoxIcon.Question),
            (Enter the default button e.g. MessageBoxDefaultButton.Button1)

            更多信息可以找到here

            【讨论】:

              【解决方案8】:

              我得到了同样的错误 'System.Windows.Forms.MessageBox' 是一个'类型',但使用起来像一个'变量',即使使用:

              MessageBox.Show("Hello, World!");
              

              我猜我最初使用无效语法的尝试导致了某种错误,我最终通过在“MessageBox.Show”和括号 () 之间添加一个空格来修复它:

              MessageBox.Show ("Hello, World!");
              

              现在使用没有多余空格的原始语法再次起作用:

              MessageBox.Show("Hello, World!");
              

              【讨论】:

              • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。
              • 这间接回答了这个问题,因为这是解决我的问题的解决方案,因为我遇到了与原始海报相同的错误。我的问题也是如何在 C# 中创建消息框,但这里的答案都没有解决我的问题。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-03-05
              • 2012-02-23
              • 1970-01-01
              • 1970-01-01
              • 2013-01-20
              • 2019-06-20
              相关资源
              最近更新 更多