zoukankan      html  css  js  c++  java
  • C# 本地txt文件读取至comboBox下拉列表

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

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

  • 相关阅读:
    设计模式--抽象工厂(个人笔记)
    C#中Trim()、TrimStart()、TrimEnd()的用法
    C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
    枚举、字符串、值之间的转换
    C# 获取文件名、目录、后缀、无后缀文件名、扩展名、根目录等
    向服务器发送Post或Get请求(封装好的)
    未能加载文件或程序集“AspNetPager”或它的某一个依赖项。参数错误。 (异常来自 HRESULT:0x80070057 (E_INVALIDARG))
    Hibernate实现向数据库插入一条数据全过程(Study By Example)
    es6 模块化
    css3 属性认识
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1762857.html
Copyright © 2011-2022 走看看