I am Reading data from serialport. I just want 40 lines to appear in the textbox.
How I can erase lines older lines to make Place for new lines?
I tried the following code:
int numOfLines = 40;
var lines = this.textBox1.Lines;
var newLines = lines.Skip(numOfLines);
this.textBox1.Lines = newLines.ToArray();
But it gives me error, saying that " 'string[]' does not contain a definition for 'Skip' and no extension method 'Skip' accepting a first argument of type 'string[]' could be found".
I think you have forgotten to add using System.Linq;
directive
PS if you want last 40 lines to be appear, you can use approach described in this question: Using Linq to get the last N elements of a collection?
您需要添加对Linq的引用:
using System.Linq;
Skip
is a extension method of LINQ. You must add a reference to System.Core
in your project, and in case it's needed a using System.Linq;
directive
Edit
As you seem to be "unable" to use LINQ, here is a non-LINQ solution (just as an experiment of reinventing the wheel):
Extension Methods
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;
}
}
Usage
this.textBox1.Lines = this.textBox1.Lines.SkipLines(2).ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.