如题,小弟菜鸟。
打个比方,比如现有3行文字在txt文件中如下:
aaaa~bbbb~cccc
dddd~eeee~dddd
ffff~gggg~hhhh
我想把它们按~分割,分别读入9个textbox,也就是aaaa读入txtbox1,bbbb读入txtbox2,cccc读入txtbox3,dddd读入txtbox4,请问怎么弄?我自己的代码是
TextBox[] fillTextboxArray = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6... };
            List<string> lst = new List<string>(File.ReadAllLines(@"C:\1.txt"));
            foreach (string s in lst)
            {
                MatchCollection fields = Regex.Matches(s, @"[^~\n]+");
                int max = Math.Min(fields.Count, fillTextboxArray.Length);
                for (int i = 0; i < max; i++)
                {
                    fillTextboxArray[i].Text = fields[i].Value;
                }可是这样不行,因为foreach进行完以后相当于只把最后一行的ffff,gggg,hhh分别填入了txtbox1,txtbox2,txtbox3那么怎么改啊。求高手给代码!!

解决方案 »

  1.   

    for(int i=1;i<lst.Count+1;i++)
    {
     string s=lst[i];
     ((TextBox)this.FindControl("textbox"+i)).Text=s;
    }
      

  2.   


                TextBox[] fillTextboxArray = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6... };
                StreamReader sr = new StreamReader(@"E:\test.txt");
                String content = sr.ReadToEnd();            String[] ss = content.Split(new char[] { '\n', '\r', '~'}, StringSplitOptions.RemoveEmptyEntries);            for (int i = 0; i < max; i++)
                {
                  fillTextboxArray[i].Text = ss[i];
                }
      

  3.   


                StreamReader sr = new StreamReader(@"E:\test.txt");
                String content = sr.ReadToEnd();            String[] ss = content.Split(new char[] { '\n', '\r', '~'}, StringSplitOptions.RemoveEmptyEntries);            for (int i = 0; i < ss.Length; i++)
                {
                  Console.WriteLine(ss[i]);
                }
    上边的代码能读出内容,并将9个字符串存在ss数组中,下边的代码只是赋值,我没建web项目,所以你自己看看,代码应该没错
                TextBox[] fillTextboxArray = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6... };            StreamReader sr = new StreamReader(@"E:\test.txt");
                String content = sr.ReadToEnd();            String[] ss = content.Split(new char[] { '\n', '\r', '~'}, StringSplitOptions.RemoveEmptyEntries);            for (int i = 0; i < ss.Length && i < fillTextboxArray.Length; i++)
                {
                  fillTextboxArray[i].Text = ss[i];
                }