0

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".

user6203007
  • 33
  • 1
  • 1
  • 8
  • 1
    Didn't you just asked the same some minutes ago? Add `using System.Linq;` to your class – Pikoh Apr 29 '16 at 09:04
  • @Pikoh Thanks :) I deleted that and came with a new question o show what code I have tried. "using System.Linq" did not work. same problem arise. – user6203007 Apr 29 '16 at 09:14
  • Do you have a reference in your proyect to `System.Core`? What .net platform target do you have? – Pikoh Apr 29 '16 at 09:21

3 Answers3

0

I think you have forgotten to add using System.Linq; directive

P.S. 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?

Community
  • 1
  • 1
hmnzr
  • 1,390
  • 1
  • 15
  • 24
0

You need to add a reference to Linq:

using System.Linq;
Ash
  • 5,786
  • 5
  • 22
  • 42
  • same problem. saying that "using directive is unnecessary" – user6203007 Apr 29 '16 at 09:28
  • Can you send me all the using directives in that files. Also what framework are you working with? – Ash Apr 29 '16 at 09:55
  • using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.IO; using System.Text.RegularExpressions; – user6203007 Apr 29 '16 at 10:03
  • try this and tell me what happens....................... var newLines = System.Linq.Enumerable.Skip(lines, numOfLines); – Ash Apr 29 '16 at 10:25
  • It says the namespace name 'Linq' does not exist in the namespace 'System'. – user6203007 Apr 29 '16 at 10:37
  • 1
    @user6203007...i've already told you, add a reference to System.Core in your project – Pikoh Apr 29 '16 at 10:44
0

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();
Pikoh
  • 7,582
  • 28
  • 53