2016-04-29 71 views
0

我正在从串口读取数据。我只想要40行出现在文本框中。删除文本框中的行。

我怎样才能擦除旧线条的线条来制作换行符?

我尝试下面的代码:

 int numOfLines = 40; 
    var lines = this.textBox1.Lines; 
    var newLines = lines.Skip(numOfLines); 
    this.textBox1.Lines = newLines.ToArray(); 

但它给我的错误,他说,“‘字符串[]’不包含‘跳过’的定义,并没有扩展方法‘跳过’接受第一可以找到'string []'类型的参数“。

+1

刚刚几分钟前你刚刚问过吗?添加'使用System.Linq;'到你的班级 – Pikoh

+0

@Pikoh谢谢:)我删除了它,并带有一个新问题o显示我尝试过的代码。 “使用System.Linq”不起作用。同样的问题出现。 – user6203007

+0

你有没有在'System.Core'的专家中参考?你有什么.NET平台的目标? – Pikoh

回答

0

我想你已经忘记了添加using System.Linq;指令

附:如果你想最后40行要出现,你可以使用这个问题描述的方法:Using Linq to get the last N elements of a collection?

+0

'拿(n)'会给第一个n元素。但最后n。 –

+0

@LeonidMalyshev对不起,错过了这个问题,将解决我的帖子 – hmnzr

+0

同样的问题。说:“使用指令是不必要的” – user6203007

0

您需要添加一个引用到LINQ:

using System.Linq; 
+0

同样的问题。说“使用指令是不必要的” – user6203007

+0

您可以向我发送该文件中的所有使用指令。你还在使用什么框架? – Ash

+0

使用系统; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text;使用System.Windows.Forms的 ;使用System.IO.Ports的 ;使用System.IO的 ; using System.Text.RegularExpressions; – user6203007

0

Skip是LINQ的扩展方法。您必须在您的项目引用添加到System.Core,并在情况下,它需要一个using System.Linq;指令

编辑

正如你似乎是“无法”使用LINQ,这里是一个非LINQ的解决方案(就像重新发明轮子)的实验:

扩展方法

public static class ExtMeth 
{ 
    public static IEnumerable<string> SkipLines(this string[] s, int number) 
    { 
     for (int i = number; i < s.Length; i++) 
     { 
      yield return s[i]; 
     } 
    } 

    public static string[] ToArray(this IEnumerable<string> source) 
    { 
     int count = 0; 
     string[] items = null; 
     foreach (string it in source) 
     { 
      count++; 
     } 
     int index = 0; 
     foreach (string item in source) 
     { 
      if (items == null) 
      { 
       items = new string[count]; 
      } 
      items[index] = item; 
      index++; 
     } 
     if (count == 0) return new string[0]; 
     return items; 
    } 
} 

使用方法

this.textBox1.Lines = this.textBox1.Lines.SkipLines(2).ToArray();