• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#本地txt文件读取至comboBox下拉列表

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在网上找了很多资料,最后通过调试,读取项目名称功能是实现了,具体的优化暂时没时间去弄

值得注意的是,页面在返回即刷新的时候要预处理清空下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一次读写一行文本,自动循环的读完所有内容

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
自己写的一个数据库操作类(C#)[原创]发布时间:2022-07-10
下一篇:
[C# Threading Handbook]笔记之一:Threading in .Net发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap