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.
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));
a source to share
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();
a source to share
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];
}
a source to share
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.
a source to share
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";
}
a source to share
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 );
a source to share
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.
a source to share