【问题标题】:Error with MessageBox.Show in C# [closed]C# 中的 MessageBox.Show 出错 [关闭]
【发布时间】:2014-03-12 05:46:47
【问题描述】:

考虑这段代码:

int total = 0;
using(var inFile = new StreamReader("text.txt"))
{
    string inValue = "";
    while ((inValue = inFile.ReadLine()) != null)
    {
        if(Int32.TryParse(inValue, out number))
        {
             total += number;
             Console.WriteLine("{0}", number);
        }
        else
            Console.WriteLine("{0} - not a number", inValue);
    }
}
Console.WriteLine("The sum is  {0}", total);

如果我这样做MessageBox.Show("{0}", number);,它会给我一个错误。为什么会这样,我该如何解决?

【问题讨论】:

  • 这是终端应用吗?这或许可以解释问题。
  • 什么错误?你喜欢尖叫——但你也喜欢提供零相关信息。不是聪明的组合。如果您收到错误,请告诉我们是什么错误。哦,也许阅读你调用的方法......

标签: c# numbers add


【解决方案1】:

The MSDN 非常清楚地指出,MessageBox.Show 确实需要两个字符串。但这两个不是格式和参数,而是文本和标题。如果要格式化文本,请使用 string.Format 在调用该方法之前格式化字符串。您也可以使用其他重载之一,但无论您使用哪种,都需要进行自己的格式化。

命名空间: System.Windows.Forms

程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)

这意味着您需要此命名空间,并且您需要引用中的 dll。默认情况下,这两种情况都不会在控制台应用程序中发生。

【讨论】:

    【解决方案2】:

    看起来你正在编写一个控制台应用程序,然后 Messagebox 就不可能了。但是,如果您实际上是在编写 Windows Forms 应用程序,那么答案如下:

    Messagebox 没有“内置”格式化程序,例如 Console.WriteLine。如果要格式化字符串,必须使用String.Format:

    MessageBox.Show(String.Format("{0}", number));
    

    或者:

    MessageBox.Show(number.ToString());
    

    【讨论】:

      【解决方案3】:

      MessageBox 在控制台应用程序中不起作用。

      【讨论】:

      • 为什么不呢?如果你能解释一下就更好了。
      【解决方案4】:

      问题出在线路上

          MessageBox.Show("{0}", number);
      

      你不能像调用 Console.writeline() 那样调用 MessageBox.show()。 MessageBox.show 中的第二个参数是 MessageBox 标题。试着写

      MessageBox.Show(number) 
      

      MessageBox.Show("The sum is "+number);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-12
        • 1970-01-01
        • 2015-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多