473,432 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

messed up listbox entries

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
3 1196
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Broder | last post by:
Hi there, I am currently running into a somewhat weired problem and hope that this NG is able to help ;-) I have a table in a MSSQL Server in which there is one Column that stores a date. The...
3
by: CJM | last post by:
I would like to be able to use an autocompleting listbox - that is, one where you can select an item in the list by typing the first few characters; the standard IE listbox only responds to the...
2
by: Filips Benoit | last post by:
Dear All, What is the max lenght of the valuelist-string as rowsource in a listbox ? I use a listbox to show all the files in a folder. Filip
3
by: Thunder$truck | last post by:
I'm sure this is easy but I can't figure it out. I use a ListBox for my application log and each time the app performs a task an entry is added into the ListBox. When the list box becomes...
1
by: Microskills | last post by:
I am a new VB.NET programmer. I am writing a dictionary program with a Dictionary file has over 60000 entries. When user starts typing a word in the searchbox, I want to show words that start with...
1
by: Microskills | last post by:
I am a new VB.NET programmer. I am writing a dictionary program with a Dictionary file has over 60000 entries. When user starts typing a word in the searchbox, I want to show words that start with...
1
by: Trevor | last post by:
Please can someone help me. I have a small project that I want to do but I am having problems trying to figure out how to do this. I have created a small application that take numbers that I give...
4
by: Thomas Adler | last post by:
Hello, I want to create a listbox with icons. Icon1 --- Item1 ----comment01 Icon1 --- Item2 ----comment01 Icon1 --- Item3 ----comment01 Icon2 --- Item4 ----comment01 Icon1 --- Item5...
1
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a ListBox on my Visual Studio C# form with several entries (14,000). I have to put a search field on the form. As text is entered into the search field, I want the ListBox to remove...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.