Results 1 to 11 of 11

Thread: Listbox: Delete empty lines...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    130

    Listbox: Delete empty lines...

    hey guys, ive been searching the whols google, and i couldnt find a ny solution.. i was wondering if someone here can help me out, i am loading a listbox items from a .txt files, and sometimes there are empty lines, there is a way i can delete them?

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Listbox: Delete empty lines...

    Try something like this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim fileContents As String = File.ReadAllText("C:\temp\test.txt")
    3.     Dim splitter() As Char = {Chr(13)}
    4.     Dim listSource() As String = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
    5.     ListBox1.DataSource = listSource
    6. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Listbox: Delete empty lines...

    Why don't you just not add the line if blank:
    With List1
    If LineToAdd <> "" then
    .addItem LineToAdd
    end with
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Listbox: Delete empty lines...

    After loading the listbox from the file:
    vb Code:
    1. Dim i As Integer = 0
    2. With Me.lstListBox.Items
    3. While i < .Count
    4.        If CStr(.Item(i)) = "" Then
    5.               .RemoveAt(i)
    6.        Else
    7.               i += 1
    8.        End If
    9. End While
    10. End With
    Last edited by minitech; May 13th, 2009 at 08:11 PM. Reason: Added "End With". Oops!

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Listbox: Delete empty lines...

    You might be able to use LINQ to do something like this:
    vb.net Code:
    1. ListBox1.Items = From o As Object In ListBox1.Items _
    2.                  Where o.ToString <> String.Empty _
    3.          Select o
    (Not tested, might not work the way I expect it to)

    However as mentioned, it is much better to not load the strings into your listbox in the first place.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Listbox: Delete empty lines...

    Quote Originally Posted by NickThissen View Post
    You might be able to use LINQ to do something like this:
    vb.net Code:
    1. ListBox1.Items = From o As Object In ListBox1.Items _
    2.                  Where o.ToString <> String.Empty _
    3.          Select o
    (Not tested, might not work the way I expect it to)

    However as mentioned, it is much better to not load the strings into your listbox in the first place.
    The Items collection of listbox can't be assigned that way because it is read only.

    Here is the correct way to do that, which is more or less equivalent of the non-LINQ version in my previous post:
    vb.net Code:
    1. ListBox1.DataSource = (From o As Object In ListBox1.Items _
    2.                       Where o.ToString <> String.Empty _
    3.                       Select o).ToList
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Listbox: Delete empty lines...

    Quote Originally Posted by Pradeep1210 View Post
    The Items collection of listbox can't be assigned that way because it is read only.

    Here is the correct way to do that, which is more or less equivalent of the non-LINQ version in my previous post:
    vb.net Code:
    1. ListBox1.DataSource = (From o As Object In ListBox1.Items _
    2.                       Where o.ToString <> String.Empty _
    3.                       Select o).ToList
    I see. I suspected that but I wasn't in any position to test it. Thanks for clarifying.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    130

    Re: Listbox: Delete empty lines...

    its not working @_@"

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Listbox: Delete empty lines...

    try this:

    vb Code:
    1. While ListBox1.FindStringExact("") <> -1
    2.     ListBox1.Items.Remove("")
    3. End While

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Listbox: Delete empty lines...

    Have you tried mine yet? It works with me.

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Listbox: Delete empty lines...

    Quote Originally Posted by -Negative- View Post
    its not working @_@"
    Which one is not working??
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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