C# 本地txt檔案讀取至comboBox下拉式清單

來源:互聯網
上載者:User

在網上找了很多資料,最後通過調試,讀取項目名稱功能是實現了,具體的最佳化暫時沒時間去弄

值得注意的是,頁面在返回即重新整理的時候要預先處理清空下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一次讀寫一行文本,自動迴圈的讀完所有內容

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.