[英]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.