By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

messed up listbox entries

P: n/a
I am trying to read a text file laid out like, "textfield1, textfield2,
etc," and only extract the first value (textfield1) to add to the listbox.
But instead of listing vertically, I get three rows across of textfield1,
which is the correct data, but not the correct format. I put a crlf in the
lines, but that did not help at all. TIA

Private Sub LoadListbox()

Try

Dim sr As StreamReader = New StreamReader("Names.txt")

Dim line As String

line = sr.ReadLine()

Do Until sr.Peek = -1

line = sr.ReadLine()

'MsgBox("Line is " & line.Length & " long")

lstNames.Items.Add(line.Substring(0, (line.IndexOf(","))) & vbCrLf)

Loop

sr.Close()

Catch E As Exception

MsgBox("The file could not be read")

MsgBox(E.Message)

End Try

End Sub

--

Patrick Sullivan
Nov 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
First, remove the vbCRLF. I am not sure what the problem is, but the line
feed will definately not help.

You are adding a string to a collection, and the line feed is ending up as
part of the string, not as a delimiter.

To be honest, the sytax of your items.add looks fine to me. It is the same
code I use to populate my listbox.

I would make one change, however. Strictly for code readability and
debugging, create a variable to hold your string and pass it to the listbox
items.add. Also write this value out to the console to make sure the
problem is not with the string itself.

dim strListBoxItem as string
strListBoxItem = line.Substring(0, (line.IndexOf(",")))
console.writeline("List box item = " & strListBoxItem)
lstNames.Items.Add(strListBoxItem )

"Patrick Sullivan" <ps****@eatel.net> wrote in message
news:3v******************************@eatel.net...
I am trying to read a text file laid out like, "textfield1, textfield2,
etc," and only extract the first value (textfield1) to add to the listbox.
But instead of listing vertically, I get three rows across of textfield1,
which is the correct data, but not the correct format. I put a crlf in the
lines, but that did not help at all. TIA

Private Sub LoadListbox()

Try

Dim sr As StreamReader = New StreamReader("Names.txt")

Dim line As String

line = sr.ReadLine()

Do Until sr.Peek = -1

line = sr.ReadLine()

'MsgBox("Line is " & line.Length & " long")

lstNames.Items.Add(line.Substring(0, (line.IndexOf(","))) & vbCrLf)

Loop

sr.Close()

Catch E As Exception

MsgBox("The file could not be read")

MsgBox(E.Message)

End Try

End Sub

--

Patrick Sullivan

Nov 23 '05 #2

P: n/a
Thanks Jim, good idea about the console output testing. I have never used
file reading with VB before, other than databases, so I just cobbled
together what I thought would work. Seems like C/C++ was so much easier.

--

Patrick Sullivan, AA-BA, BA-IT

"Jim Underwood" <ja*************@fallonclinic.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
First, remove the vbCRLF. I am not sure what the problem is, but the line
feed will definately not help.

You are adding a string to a collection, and the line feed is ending up as
part of the string, not as a delimiter.

To be honest, the sytax of your items.add looks fine to me. It is the same code I use to populate my listbox.

I would make one change, however. Strictly for code readability and
debugging, create a variable to hold your string and pass it to the listbox items.add. Also write this value out to the console to make sure the
problem is not with the string itself.

dim strListBoxItem as string
strListBoxItem = line.Substring(0, (line.IndexOf(",")))
console.writeline("List box item = " & strListBoxItem)
lstNames.Items.Add(strListBoxItem )

"Patrick Sullivan" <ps****@eatel.net> wrote in message
news:3v******************************@eatel.net...
I am trying to read a text file laid out like, "textfield1, textfield2,
etc," and only extract the first value (textfield1) to add to the listbox. But instead of listing vertically, I get three rows across of textfield1, which is the correct data, but not the correct format. I put a crlf in the lines, but that did not help at all. TIA

Private Sub LoadListbox()

Try

Dim sr As StreamReader = New StreamReader("Names.txt")

Dim line As String

line = sr.ReadLine()

Do Until sr.Peek = -1

line = sr.ReadLine()

'MsgBox("Line is " & line.Length & " long")

lstNames.Items.Add(line.Substring(0, (line.IndexOf(","))) & vbCrLf)

Loop

sr.Close()

Catch E As Exception

MsgBox("The file could not be read")

MsgBox(E.Message)

End Try

End Sub

--

Patrick Sullivan


Nov 23 '05 #3

P: n/a
Now I feel dumb. I was using some control called listview instead of
listbox. Now it works fine. Thanks!

--

Patrick Sullivan, AA-BA, BA-IT

"Jim Underwood" <ja*************@fallonclinic.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
First, remove the vbCRLF. I am not sure what the problem is, but the line
feed will definately not help.

You are adding a string to a collection, and the line feed is ending up as
part of the string, not as a delimiter.

To be honest, the sytax of your items.add looks fine to me. It is the same code I use to populate my listbox.

I would make one change, however. Strictly for code readability and
debugging, create a variable to hold your string and pass it to the listbox items.add. Also write this value out to the console to make sure the
problem is not with the string itself.

dim strListBoxItem as string
strListBoxItem = line.Substring(0, (line.IndexOf(",")))
console.writeline("List box item = " & strListBoxItem)
lstNames.Items.Add(strListBoxItem )

"Patrick Sullivan" <ps****@eatel.net> wrote in message
news:3v******************************@eatel.net...
I am trying to read a text file laid out like, "textfield1, textfield2,
etc," and only extract the first value (textfield1) to add to the listbox. But instead of listing vertically, I get three rows across of textfield1, which is the correct data, but not the correct format. I put a crlf in the lines, but that did not help at all. TIA

Private Sub LoadListbox()

Try

Dim sr As StreamReader = New StreamReader("Names.txt")

Dim line As String

line = sr.ReadLine()

Do Until sr.Peek = -1

line = sr.ReadLine()

'MsgBox("Line is " & line.Length & " long")

lstNames.Items.Add(line.Substring(0, (line.IndexOf(","))) & vbCrLf)

Loop

sr.Close()

Catch E As Exception

MsgBox("The file could not be read")

MsgBox(E.Message)

End Try

End Sub

--

Patrick Sullivan


Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.