Populating a TextBox from a text file

I am trying to get certain lines from a file and put them on another line, or maybe if we put it in a textbox it won't be a problem: P

string[] msglines;

msglines = System.IO.File.ReadAllLines(@"C:\\Users\xA\Desktop\MESSAGES.txt");

for (int x = 0; x < msglines.Length; x++)
{
    this.textBox5.Text = msglines[c];
    c = c + 2;
}

      

I get: The index was outside the array.

+1


a source to share


13 replies


If you want to get every second line.

Change your loop to

//Odd Lines
for (int x = 0; x < msglines.Length; x = x + 2)
{
    this.textBox5.Text += msglines[x];
}

//Even Lines
for (int x = 1; x < msglines.Length; x = x + 2)
{
    this.textBox5.Text += msglines[x];
}

      



As noted in the comments, you can shorten x = x + 2

tox += 2

And in the interests of LinqY Goodness ...

//ODDS
msgLines
    .Where((str, index) => index % 2 == 0)
    .ToList()
    .ForEach(str => textBox1.Text += String.Format("{0}\r\n", str));

//EVENS
msgLines
    .Where((str, index) => index % 2 == 1)
    .ToList()
    .ForEach(str => textBox1.Text += String.Format("{0}\r\n", str));

      

+7


a source


your loop is driven by x, but you are indexing with c [hence you need logic so that c does not grow larger than the size of the array)



+3


a source


Since you are incrementing the row index ( c

) by 2 each time; just use x

:

this.textBox5.Text = msglines[x];

      

Of course, from within the loop, this will only display the last line. Which line do you really want to show?


Edit comment; in this case, simply:

StringBuilder sb = new StringBuilder();
for (int x = 1; x < msglines.Length; x+=2)
{
    sb.AppendLine(msglines[x]);
}
this.textBox5.Text = sb.ToString();

      

+3


a source


In your for loop, you use c. You need to use x. May I suggest you look at the link for .

Try this instead ...

string[] msglines;

msglines = System.IO.File.ReadAllLines(@"C:\\Users\xA\Desktop\MESSAGES.txt");


for (int x = 0; x < msglines.Length; x++)
{
    this.textBox5.Text = msglines[x];                       
}

      

+2


a source


Then you have to use X as an index with a step of 2 instead of 1.

+1


a source


For simplicity's sake, I'll use an example of taking any other character from a string, but the case of taking every other string from an array of strings is similar.

Let's take "Hello" as an example for your msglines

alt text http://www.javabat.com/doc/string_index.png

and let's say your global variable c i is initialized to 0 before you enter the for loop. Your msglines.Lenght will be 5. On the fourth iteration of the loop (x == 3) you will try to access msglines [6] which is outside the bounds of the array, hence an error.

You probably wanted something along the lines

int x = 0;
while(x <= msglines.Lenght){
  this.textBox5.Text += msglines[x];
  x = x + 2;
}

      

or

for(x=0; x <= msglines.Lenght ; x+=2){
  this.textBox5.Text += msglines[x];
}

      

To get odd lines, you start with x initialized to 1.

As for which of the two fully equivalent versions above to use, I would suggest using the one that is more readable for you. In my experience, this is always the best choice.

+1


a source


try it

string[] msglines;

 msglines = System.IO.File.ReadAllLines(@"C:\Users\xA\Desktop\MESSAGES.txt");

 for (int x = 0; x < msglines.Length; x++)
  {

      this.textBox5.Text = msglines[x++];

  }                   

      

+1


a source


You say you want all odd-numbered lines in the TextBox, but your loop will only show the last line as you are overwriting the content on each pass.

To show all odd numbered lines separated by line breaks:

string lineSep = "";
for (int x = 0; x < msglines.Length; x += 2)
{
    this.textBox5.Text += lineSep + msglines[x];
    lineSep = "\r\n";
}

      

+1


a source


You have double backslashes in your call ReadAllLines()

, causing the file to fail to open.

Also, you are using c

as array index, it looks like you wantx

0


a source


The code I wrote is exactly how I want it to be .....

I really want every odd line from my file to be sent to the textbox. And yes, PS C initializes from 1

0


a source


Apart from the already mentioned problems with indexing c

versus x

, you also overwrite textBox5.Text

on each iteration, so you only see the last line. It seems somewhat unlikely that this would be the intended behavior. Instead, you might want something like this:

this.textBox5.Text += msglines[x]; // note x instead of c

      

Or, if you really want everything and +2 is a typo in itself, you can do it without any loop:

this.textBox5.Text = String.Join( "\n", msglines );

      

0


a source


The following function will return IEnumerable, which gives the odd numbered lines in the file:

private IEnumerable<string> GetOddLines(string fileName)
{
    string[] lines = File.ReadAllLines(fileName);
    IEnumerator allLines = lines.GetEnumerator();
    while (allLines.MoveNext())
    {
        yield return (string)allLines.Current;
        if (!allLines.MoveNext())
        {
            break;
        }
    }
}

      

If you want even lines, just add a call to allLines.MoveNext () before the while loop.

0


a source


You need to dump an array with length -1. The 45 Length array will only index up to 44.

0


a source







All Articles