comboBox如何获取(新建文本文档)每行的数据?

解决方案 »

  1.   

    streamreader去readline,然后将readline的内容添加到combox的items
      

  2.   

    按行读文本放到comboBox里System.IO.StreamReader sr = new System.IO.StreamReader("c:\\A.txt", System.Text.Encoding.Default);
            string strLine = sr.ReadLine();
            while (strLine != null)
            {
                comboBox.Items.Add(strLine);
                strLine = sr.ReadLine();
            }
            sr.Close();
      

  3.   

    您好,刚才测试了下是可以的,请问下 获取后让comboBox直接选中刚才文本档里首个值如何实现?
      

  4.   


    System.IO.StreamReader sr = new System.IO.StreamReader("c:\\A.txt", System.Text.Encoding.Default);
            string strLine = sr.ReadLine();
            while (strLine != null)
            {
                comboBox.Items.Add(strLine);
                strLine = sr.ReadLine();
            }
            sr.Close();
      if(comboBox.Items.Count>0)  //默认选中第一个
         comboBox.SelectedIndex=0; 
      

  5.   

     File.ReadAllLines("D:\\1.txt", Encoding.Default).ToList().ForEach(t => comboBox1.Items.Add(t));
                if (comboBox1.Items.Count > 0)
                    comboBox1.SelectedIndex = 0;
      

  6.   

    刚才我发现了个小问题,就是我连续点击button的时候 获取了两次数据
      是不是点击button的时候 做个判断?
    点击button的时候先判断comboBox里是否有值
    如果没有值 那就填充进去,如果有值就循环遍历比对下 有的就不添加,没有的就添加
    这样的思路有问题吗?
      

  7.   

    点击button的时候,先清空下拉框内容            comboBox1.Items.Clear();
      

  8.   


    System.IO.StreamReader sr = new System.IO.StreamReader("c:\\A.txt", System.Text.Encoding.Default);
            string strLine = sr.ReadLine();
            while (strLine != null)
            {
                comboBox.Items.Add(strLine);
                strLine = sr.ReadLine();
            }
            sr.Close();
      if(comboBox.Items.Count>0)  //默认选中第一个
         comboBox.SelectedIndex=0; 
      

  9.   

    comboBox.Items.Clear(); //先清除,再加就没事了
    System.IO.StreamReader sr = new System.IO.StreamReader("c:\\A.txt", System.Text.Encoding.Default);
             string strLine = sr.ReadLine();
             while (strLine != null)
             {
                 comboBox.Items.Add(strLine);
                 strLine = sr.ReadLine();
             }
             sr.Close();
       if(comboBox.Items.Count>0)  //默认选中第一个
          comboBox.SelectedIndex=0;