Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4012636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:16:19+00:00 2026-05-20T09:16:19+00:00

private void sessionText() { try { System.IO.TextReader r = new System.IO.StreamReader(saved.txt); this.textBox1.Text = r.ReadLine();

  • 0
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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:16:20+00:00Added an answer on May 20, 2026 at 9:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method: private delegate void watcherReader(StreamReader sr); private void watchProc(StreamReader sr) {
private void ShowIterationSelection() { IterationForm iterationForm = new IterationForm(lblProjectID.Text); iterationForm.ShowDialog(); } When I show
private void textBox1_MouseClick(object sender, MouseEventArgs e) { textBox1.SelectAll(); } This works but I have
private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return
. private void Form1_Load(object sender, EventArgs e) { List<CaclulatedData> tests = new List<CaclulatedData> {
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
This code compiles: private static void Main(string[] args) { bool? fred = true; if
private void button1_Click(object sender, EventArgs e) { string name; name = textBox5.Text; SqlConnection con10
private void NuestroButton1_Click(object sender, RoutedEventArgs e) { if //the sender's .Text/.Content is X {
private void EnsureCurrentlyValid() { //I'm valid if IDataErrorInfo.this[] returns null for every property var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.