-
May 11th, 2009, 12:55 PM
#1
Thread Starter
Addicted Member
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?
-
May 11th, 2009, 01:58 PM
#2
Re: Listbox: Delete empty lines...
Try something like this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fileContents As String = File.ReadAllText("C:\temp\test.txt") Dim splitter() As Char = {Chr(13)} Dim listSource() As String = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries) ListBox1.DataSource = listSource End Sub
Pradeep
-
May 11th, 2009, 02:04 PM
#3
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
-
May 11th, 2009, 02:45 PM
#4
Re: Listbox: Delete empty lines...
After loading the listbox from the file:
vb Code:
Dim i As Integer = 0
With Me.lstListBox.Items
While i < .Count
If CStr(.Item(i)) = "" Then
.RemoveAt(i)
Else
i += 1
End If
End While
End With
Last edited by minitech; May 13th, 2009 at 08:11 PM.
Reason: Added "End With". Oops!
-
May 11th, 2009, 03:16 PM
#5
Re: Listbox: Delete empty lines...
You might be able to use LINQ to do something like this:
vb.net Code:
ListBox1.Items = From o As Object In ListBox1.Items _ Where o.ToString <> String.Empty _ 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.
-
May 11th, 2009, 03:23 PM
#6
Re: Listbox: Delete empty lines...
Originally Posted by NickThissen
You might be able to use LINQ to do something like this:
vb.net Code:
ListBox1.Items = From o As Object In ListBox1.Items _
Where o.ToString <> String.Empty _
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:
ListBox1.DataSource = (From o As Object In ListBox1.Items _
Where o.ToString <> String.Empty _
Select o).ToList
-
May 11th, 2009, 03:24 PM
#7
Re: Listbox: Delete empty lines...
Originally Posted by Pradeep1210
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:
ListBox1.DataSource = (From o As Object In ListBox1.Items _
Where o.ToString <> String.Empty _
Select o).ToList
I see. I suspected that but I wasn't in any position to test it. Thanks for clarifying.
-
May 13th, 2009, 04:03 PM
#8
Thread Starter
Addicted Member
Re: Listbox: Delete empty lines...
-
May 13th, 2009, 05:53 PM
#9
Re: Listbox: Delete empty lines...
try this:
vb Code:
While ListBox1.FindStringExact("") <> -1
ListBox1.Items.Remove("")
End While
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 13th, 2009, 08:11 PM
#10
Re: Listbox: Delete empty lines...
Have you tried mine yet? It works with me.
-
May 14th, 2009, 03:31 AM
#11
Re: Listbox: Delete empty lines...
Originally Posted by -Negative-
its not working @_@"
Which one is not working??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|