2016-04-29 72 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();