开发者

loading data into each textbox line by line

开发者 https://www.devze.com 2023-02-13 22:03 出处:网络
private void sessionText() { try { System.IO.TextReader r = new System.IO.StreamReader(\"saved.txt\"); this.textBox1.Text = r.ReadLine();
private void sessionText()
        {
            try
            {
                System.IO.TextReader r = new System.IO.StreamReader("saved.txt");
                this.textBox1.Text = r.ReadLine();
                r.Close();
            }
            catch (Exception x)
            {
                MessageBox.Show("Exception " +x);
            }
        }

It reads the line into textBox1 but now I'm expanding my application. I added 5 more textBoxes and now开发者_运维知识库 I'm trying to load the data in each one saved line by line. How can I load each line into the next textbox?

Line 0 -> textBox1
Line 1 -> textBox2
Line 2 -> textBox3


If you don't want this to be a generic method then simply call the ReadLine method again and assign the result to textBox2.Text and so on:

    private void sessionText()
    {
        try
        {
            using (System.IO.TextReader r = new System.IO.StreamReader("saved.txt"))
            {
              this.textBox1.Text = r.ReadLine();
              this.textBox2.Text = r.ReadLine();
              this.textBox3.Text = r.ReadLine();
            }
        }
        catch (Exception x)
        {
            MessageBox.Show("Exception " +x);
        }
    }

Note that the TextReader should be Disposed. That is you need to call it's Close() or Dispose() method because it is an IDisposable. In fact any object that implements the IDisposable interface must to disposed.

you normally do this using the "using" construct. But in your case, because you've also got a try-catch, you could do it like this:

private void button1_Click(object sender, EventArgs e)
{
  System.IO.TextReader reader = null;      
  try
  {
    reader = new System.IO.StreamReader("saved.txt");
    textBox1.Text = reader.ReadLine();
    textBox2.Text = reader.ReadLine();
    textBox3.Text = reader.ReadLine();
  }
  catch (Exception x)
  {
    MessageBox.Show("Exception " + x);
  }
  finally
  {
    if (reader != null)
      reader.Close();
  }
}

There is an assumption you're making with this code though. By this code I mean all of the code I've listed as well as the code you've listed and that is, there are guaranteed to be 3 lines of text in the file you're reading. Not this might always be the case (in your situation, but you may want to look at a more defensive approach to this kind of thing.

Also, you've already had to modify the code from 2 line and 1 textbox to 3 lines and 3 textboxes. Maybe it's time to look at implementing this in such as way that the next time (when your requirements go from say 3 to 10) you don't have to modify your code but rather let it know (some how) about the additional textboxes and it should do the right thing? Or maybe there is something in your application that knows the number of "lines" (whatever they signify) and you could use this information?

Just a thought.


Try this code

using (StreamReader sr = new StreamReader(@"saved.txt"))
{
    string[] inputData = sr.ReadToEnd().Split(new string[] { "\n","\r","\n\r" }, 
        StringSplitOptions.RemoveEmptyEntries);
    var textboxes = (from Control textbox in this.Controls
                        where textbox is TextBox
                        select textbox).ToArray();
    for (int i = 0; i < textboxes.Count(); i++)
    {
        textboxes[i].Text = inputData[i];
    }
}


Like Anuraj, suggester, the best way is to create an array of textBoxes and fill them with every line readed using a cycle. But his suggestion implies that the LinQ selection returns textBoxes in the exact order you want. maybe with several tries you can do that.

Try this: in the _Load of the form prepare the array:

Texts = new TextBox[10];
Texts[0] = txtBox1;
Texts[1] = txtBox2;

etcetera.

After that you can read line by line in a cycle and assign to Texts[cnt]

This is also a workaroud after .NET removed the the Index property of VB6 so that to create a control array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号