Results 1 to 7 of 7

Thread: [RESOLVED] Deleting repeated Lines in Textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Resolved [RESOLVED] Deleting repeated Lines in Textbox

    Hi all.

    If I have a multi-line textbox like this:

    ----

    Line 1
    Line 1
    Line 1
    Line 2
    Line 2
    Line 3

    ----

    How do I remove lines that are the same as the line above?

    This is my code:

    vb.net Code:
    1. For i = 0 To txtTextResult.Lines.Count - 1
    2.             Dim sLine As String
    3.             sLine = txtTextResult.Lines(i)
    4.             Try
    5.                 Dim sNextLine As String = txtTextResult.Lines(i + 1)
    6.                 If sNextLine = sLine Then
    7.                     'Remove txtTextResult.Lines(i + 1)
    8.                    
    9.                 End If
    10.             Catch : Exit For : End Try 'When sLine is the last line, sNextLine can't be the line under it.
    11.         Next

    I've already tried many things.

    For example, this, which is not working:

    vb.net Code:
    1. txtTextResult.Lines(i + 1).Remove(0) 'Remove ( startindex as integer)

    Thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Deleting repeated Lines in Textbox

    try this:

    vb Code:
    1. Dim lines As New List(Of String)(txtTextResult.Lines)
    2. For x As Integer = lines.Count - 1 To 0 Step -1
    3.     Dim currentLine As String = lines(x)
    4.     Dim matchCount = Aggregate line As String In lines _
    5.                 Where line = currentLine Into Count()
    6.     For m As Integer = 1 To matchCount - 1
    7.         lines.Remove(currentLine)
    8.     Next
    9. Next
    10.  
    11. txtTextResult.Lines = lines.ToArray

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deleting repeated Lines in Textbox

    I don't think .paul.'s solution will work because it would remove duplicates even if they weren't adjacent. Try this:
    vb.net Code:
    1. Dim lines As New List(Of String)(txtTextResult.Lines)
    2.  
    3. For index As Integer = lines.Count - 1 To 1 Step -1
    4.     If lines(index) = lines(index - 1) Then
    5.         lines.RemoveAt(index)
    6.     End If
    7. Next
    8.  
    9. txtTextResult.Lines = lines.ToArray()
    Note that the code reads pretty much exactly as you described the requirement: if a line is the same as the line above, remove that line. Note, also that the loop goes from the end to the beginning, allowing you to remove items without affecting the indexes of those items yet to be checked. Finally, note that the last index used is 1 rather than 0, because you can't compare the first line with the line above it.
    Last edited by jmcilhinney; Apr 8th, 2010 at 12:29 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Re: Deleting repeated Lines in Textbox

    Thank you for the reply,
    it works well and I was stuck at this easy piece of code.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deleting repeated Lines in Textbox

    Quote Originally Posted by Cooi View Post
    Thank you for the reply,
    it works well and I was stuck at this easy piece of code.
    The secret is looping backwards. Whenever you want to remove items from a collection in a loop you pretty much always have to loop backwards.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Addicted Member
    Join Date
    Jun 2011
    Posts
    137

    Re: [RESOLVED] Deleting repeated Lines in Textbox

    Hi, i'm new here.
    The problem which i need is how to remove dublicates from this code ?
    I have tried many times to find how to remove dublicates in textbox but no luck ;(
    So thought maybe you can help me because i can't find an answers to this question.

    Here is my code.

    Code:
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
    
            For Each curElement As HtmlElement In theElementCollection
    
                If curElement.GetAttribute("href").Contains("xvideos") Then
    
                    TextBox2.Text += curElement.GetAttribute("innerText") & vbCrLf
    
                End If
    
            Next
    
     End Sub

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Deleting repeated Lines in Textbox

    Quote Originally Posted by polas View Post
    Hi, i'm new here.
    The problem which i need is how to remove dublicates from this code ?
    I have tried many times to find how to remove dublicates in textbox but no luck ;(
    So thought maybe you can help me because i can't find an answers to this question.

    Here is my code.

    Code:
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
    
            For Each curElement As HtmlElement In theElementCollection
    
                If curElement.GetAttribute("href").Contains("xvideos") Then
    
                    TextBox2.Text += curElement.GetAttribute("innerText") & vbCrLf
    
                End If
    
            Next
    
     End Sub
    you should start your own thread for this question

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width