简体   繁体   English

从文本文件填充TextBox

[英]Populating a TextBox from a text file

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. 我得到一个:索引超出了数组的范围。

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善良的利益...

//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));

您的循环由x控制,但您通过c进行索引[因此,您需要逻辑来防止c变得大于数组的大小]

Because you are increasing the line index ( c ) by 2 each time; 因为您每次都将行索引( c )增加2; just use x : 只需使用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();

In your for loop, you are using c. 在for循环中,您正在使用c。 You need to use x. 您需要使用x。 Could I suggest that you have a look at the reference for 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];                       
}

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

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字符串

替代文字
(source: javabat.com ) (来源: javabat.com

and let us say that your global variable ci initialized to 0 before you enter the for loop. 并说您的全局变量ci在进入for循环之前初始化为0。 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. 您的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. 根据我的经验,这始终是更好的选择。

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

  }                   

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

You need to loop the array with Length -1. 您需要使用长度-1循环数组。 Array of 45 Length will only index to 44. 长度为45的数组只能索引为44。

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

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 PS C initializes from 1 我实际上希望将文件中的所有奇数行发送到文本框。是的,PS C从1初始化

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. 除了已经提到的cx索引问题之外,您还在每次迭代中覆盖textBox5.Text ,因此您只会看到最后一行。 It seems somehow unlikely to be intended behaviour. 似乎不太可能是预期的行为。 You might want something like this instead: 您可能需要这样的东西:

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

The following function will return an IEnumerable that gives the odd numbered lines in a file: 以下函数将返回一个IEnumerable,该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()的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM