从文本文件填充TextBox

时间:2023-01-28 18:03:45

Im trying to get specific lines from a file and put them in a another string or maybe if we can put it in anothe textbox it wont be a prob :P

我试图从文件中获取特定的行并将它们放在另一个字符串中,或​​者如果我们可以将它放在另一个文本框中,它将不是一个问题: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 a : Index was outside the bounds of the array.

我得到一个:索引超出了数组的范围。

13 个解决方案

#1


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 was pointed out in the comments, you can shorten x = x + 2 to x += 2

正如评论中指出的那样,您可以将x = x + 2缩短为x + = 2

And in the interest of LinqY Goodness...

为了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));

#2


your loop is controlled by x, but you're indexing by c [hence you need logic to prevent c getting greater than size of the array]

你的循环由x控制,但是你用c编制索引[因此你需要逻辑来防止c变得大于数组的大小]

#3


Because you are increasing the line index (c) by 2 each time; just use x:

因为你每次都将线索引(c)增加2;只需使用x:

this.textBox5.Text = msglines[x];

Of course, from a loop this will result in just the last line being shown. What line is it you actually want to show?

当然,从循环开始,这将导致显示最后一行。你真的想要展示什么线?


Edit re comment; in which case, simply:

编辑重新评论;在这种情况下,简单地说:

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

#4


In your for loop, you are using c. You need to use x. Could I suggest that you have a look at the reference for for.

在你的for循环中,你正在使用c。你需要使用x。我可以建议您查看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];                       
}

#5


Then you should use X as an index, with a step of 2, instead of 1.

然后你应该使用X作为索引,步长为2,而不是1。

#6


Try this

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++];

  }                   

#7


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

为简单起见,我将使用从字符串中获取每个其他字符的示例,但是从字符串数组中获取每个其他行的情况类似。

Lets take "Hello" as an example for your msglines string

让我们以“Hello”作为msglines字符串的示例

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

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

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

让我们说你的全局变量c i在你进入for循环之前初始化为0。你的msglines.Lenght将是5.在循环的第四次迭代(x == 3)中,你将尝试访问msglines [6],它超出了数组的范围,因此出错。

You probably wanted something along the lines of

你可能想要一些东西

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 the odd lines you would start with x initialized to 1.

要获得奇数行,您将从x初始化为1开始。

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

至于上面两个完全等效的版本中的哪一个,我建议使用一个更易读的版本。根据我的经验,这总是更好的选择。

#8


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

你说你想要TextBox中的所有奇数行,但是你的循环只显示最后一行,因为你覆盖每次传递的内容。

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";
}

#9


You have a double backslash in the ReadAllLines() call, causing it to fail opening the file.

您在ReadAllLines()调用中有一个双反斜杠,导致它无法打开文件。

Also, you are using c as an array index, it looks like you want x

此外,您使用c作为数组索引,它看起来像你想要x

#10


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

我写的代码正是我想要的......

I actually want every odd line from my file to be sent to the textbox.and yeah P.S C initializes from 1

我实际上希望从我的文件中的每个奇数行发送到文本框。是的P.S C从1初始化

#11


Besides the already mentioned indexing problems of c versus x, you are also overwriting textBox5.Text on every iteration, so you'll only ever see the last line. It seems somehow unlikely to be intended behaviour. You might want something like this instead:

除了已经提到的c与x的索引问题之外,你还会在每次迭代时覆盖textBox5.Text,所以你只会看到最后一行。似乎不太可能是预期的行为。您可能想要这样的东西:

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

Or if you really want everything and the +2 was a typo in itself you could do this without any loop:

或者如果你真的想要一切并且+2本身就是一个拼写错误你可以在没有任何循环的情况下做到这一点:

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

#12


The following function will return an IEnumerable that gives the odd numbered lines in a file:

以下函数将返回一个IEnumerable,它给出了文件中的奇数行:

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 the even numbered lines, just add a call to allLines.MoveNext() before the while loop.

如果你想要偶数行,只需在while循环之前添加对allLines.MoveNext()的调用。

#13


You need to loop the array with Length -1. Array of 45 Length will only index to 44.

您需要使用Length -1循环数组。 45长度的数组仅索引到44。

#1


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 was pointed out in the comments, you can shorten x = x + 2 to x += 2

正如评论中指出的那样,您可以将x = x + 2缩短为x + = 2

And in the interest of LinqY Goodness...

为了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));

#2


your loop is controlled by x, but you're indexing by c [hence you need logic to prevent c getting greater than size of the array]

你的循环由x控制,但是你用c编制索引[因此你需要逻辑来防止c变得大于数组的大小]

#3


Because you are increasing the line index (c) by 2 each time; just use x:

因为你每次都将线索引(c)增加2;只需使用x:

this.textBox5.Text = msglines[x];

Of course, from a loop this will result in just the last line being shown. What line is it you actually want to show?

当然,从循环开始,这将导致显示最后一行。你真的想要展示什么线?


Edit re comment; in which case, simply:

编辑重新评论;在这种情况下,简单地说:

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

#4


In your for loop, you are using c. You need to use x. Could I suggest that you have a look at the reference for for.

在你的for循环中,你正在使用c。你需要使用x。我可以建议您查看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];                       
}

#5


Then you should use X as an index, with a step of 2, instead of 1.

然后你应该使用X作为索引,步长为2,而不是1。

#6


Try this

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++];

  }                   

#7


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

为简单起见,我将使用从字符串中获取每个其他字符的示例,但是从字符串数组中获取每个其他行的情况类似。

Lets take "Hello" as an example for your msglines string

让我们以“Hello”作为msglines字符串的示例

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

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

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

让我们说你的全局变量c i在你进入for循环之前初始化为0。你的msglines.Lenght将是5.在循环的第四次迭代(x == 3)中,你将尝试访问msglines [6],它超出了数组的范围,因此出错。

You probably wanted something along the lines of

你可能想要一些东西

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 the odd lines you would start with x initialized to 1.

要获得奇数行,您将从x初始化为1开始。

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

至于上面两个完全等效的版本中的哪一个,我建议使用一个更易读的版本。根据我的经验,这总是更好的选择。

#8


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

你说你想要TextBox中的所有奇数行,但是你的循环只显示最后一行,因为你覆盖每次传递的内容。

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";
}

#9


You have a double backslash in the ReadAllLines() call, causing it to fail opening the file.

您在ReadAllLines()调用中有一个双反斜杠,导致它无法打开文件。

Also, you are using c as an array index, it looks like you want x

此外,您使用c作为数组索引,它看起来像你想要x

#10


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

我写的代码正是我想要的......

I actually want every odd line from my file to be sent to the textbox.and yeah P.S C initializes from 1

我实际上希望从我的文件中的每个奇数行发送到文本框。是的P.S C从1初始化

#11


Besides the already mentioned indexing problems of c versus x, you are also overwriting textBox5.Text on every iteration, so you'll only ever see the last line. It seems somehow unlikely to be intended behaviour. You might want something like this instead:

除了已经提到的c与x的索引问题之外,你还会在每次迭代时覆盖textBox5.Text,所以你只会看到最后一行。似乎不太可能是预期的行为。您可能想要这样的东西:

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

Or if you really want everything and the +2 was a typo in itself you could do this without any loop:

或者如果你真的想要一切并且+2本身就是一个拼写错误你可以在没有任何循环的情况下做到这一点:

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

#12


The following function will return an IEnumerable that gives the odd numbered lines in a file:

以下函数将返回一个IEnumerable,它给出了文件中的奇数行:

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 the even numbered lines, just add a call to allLines.MoveNext() before the while loop.

如果你想要偶数行,只需在while循环之前添加对allLines.MoveNext()的调用。

#13


You need to loop the array with Length -1. Array of 45 Length will only index to 44.

您需要使用Length -1循环数组。 45长度的数组仅索引到44。