在網上找了很多資料,最後通過調試,讀取項目名稱功能是實現了,具體的最佳化暫時沒時間去弄
值得注意的是,頁面在返回即重新整理的時候要預先處理清空下comboBox的值,否則會引起重複
this.combox.Items.Clear();
代碼如下:
view plaincopy to clipboardprint?
/// <summary>
/// 從txt檔案中得到項目名稱,再顯示到comboBox上
/// </summary>
private void ConvertTxtToDataSet()
{
string ReadLine;
string[] array;
string Path = @"E:\1.TXT";
StreamReader reader = new StreamReader(Path,
System.Text.Encoding.GetEncoding("GB2312"));
while (reader.Peek() >= 0)
{
try
{
ReadLine = reader.ReadLine();
if (ReadLine != "")
{
ReadLine = ReadLine.Replace("\"", "");
array = ReadLine.Split(',');
if (array.Length == 0)
{
MessageBox.Show("您選擇的匯入資料類型有誤,請重試!");
return;
}
this.comboxQcProName.Items.Add(array[0]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
/// <summary>
/// 從txt檔案中得到項目名稱,再顯示到comboBox上
/// </summary>
private void ConvertTxtToDataSet()
{
string ReadLine;
string[] array;
string Path = @"E:\1.TXT";
StreamReader reader = new StreamReader(Path,
System.Text.Encoding.GetEncoding("GB2312"));
while (reader.Peek() >= 0)
{
try
{
ReadLine = reader.ReadLine();
if (ReadLine != "")
{
ReadLine = ReadLine.Replace("\"", "");
array = ReadLine.Split(',');
if (array.Length == 0)
{
MessageBox.Show("您選擇的匯入資料類型有誤,請重試!");
return;
}
this.comboxQcProName.Items.Add(array[0]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
期中TXT裡面檔案1.txt格式為:
白細胞,"JEF",205.00,0.0351
綠細胞,"JEF",7516.00,0.0382
黑細胞,"JEF",1681.00,0.0675
藍細胞,"JEF",213.00,0.0563
結論:該函數自動讀取檔案txt一次讀寫一行文本,自動迴圈的讀完所有內容