[英]Problems with MessageBox.Show()
我是新手代码,大多数工作都有效,但我无法运行此代码。 有人可以帮忙吗?
我尝试using System.Forms
但它显示缺少命名空间。 当我using System.Windows.Forms
,该消息消失了。 它不允许我同时使用它们。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(@"file.csv");
// for set encoding
// StreamReader sr = new StreamReader(@"file.csv", Encoding.GetEncoding(1250));
string strline = "";
String[] _values = null;
int x = 0;
while(!sr.EndOfStream)
{
strline = sr.ReadLine();
_values = strline.Split(',');
if (_values.Length >= 6 && _values[0].Trim().Length > 0)
{
MessageBox.show(_values[1]);
}
}
sr.Close();
}
}
}
没有这样的命名空间System.Forms
,您尝试使用的类( MessageBox
)在System.Windows.Forms
。 通过更正您的using
语句,错误就消失了。
请记住,您必须在控制台应用程序中引用System.Windows.Forms.dll
才能使用此类。
您需要在项目中引用System.Windows.Forms.dll
。 这是一个如何做到的详细说明。
System.Forms没有这样的命名空间,只有一个名为System.Windows.Forms的命名空间,它有你正在谈论的MessageBox
类。 为了能够使用它,您需要将System.Windows.Forms.dll的引用添加到您的项目(在“添加引用...”对话框的.NET选项卡中找到它),它将起作用。 另请注意, MessageBox.Show()
需要大写'S'。 请参阅下面的代码的优化和完全工作版本。
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
using (StreamReader sr = new StreamReader(@"file.csv"))
{
while (sr.Peek() >= 0)
{
string strline = sr.ReadLine();
string[] values = strline.Split(',');
if (values.Length >= 6 && values[0].Trim().Length > 0)
{
MessageBox.Show(values[1]);
}
}
}
}
}
}
您尝试在控制台应用程序中使用它首先应在引用中添加System.Windows.Forms
dll(来自.Net引用选项卡),然后通过添加它的命名空间来使用它。
我在这里有点困惑。 没有名为System.Forms
命名空间。 它始终是System.Windows.Forms
。 MessageBox类在System.Windows.Forms
定义
您需要手动为System.Windows.Forms
添加对项目的引用,因为您在控制台应用程序而不是Windows应用程序上。 只需添加参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.