Remove Carriage Return Character from Combobox

mattkw80

Well-known member
Joined
Jan 3, 2008
Messages
49
Location
Ontario, Canada
Programming Experience
Beginner
I am populating a Combox by reading lines into it from a text file.

After every item in the Combobox - there are 2 small squares, which I would guess would be a Carriage Return.

Is there anyway I can filter the squares off, so the user does not see them in the drop down?
 
The issue would be how you're splitting the lines of the file in the first place. As you haven't shown us how you're doing that, we can only guess. Assuming that you have you just want a one-to-one mapping of lines in the file to items in the drop-down list, this is the most sensible way to load the data:
myComboBox.DataSource = IO.File.ReadAllLines(filePath)
If you don't want to bind then you can do this:
myComboBox.Items.AddRange(IO.File.ReadAllLines(filePath))
One notable difference is that binding will select the first item by default while loading items directly won't.
 
'Read Textfile into Combobox
Dim FILE_NAME As String = "C:\Data\shippers.txt"
Dim TextLine As String

If System.IO.File.Exists(FILE_NAME) = True Then

Dim objReader As New System.IO.StreamReader(FILE_NAME)

Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & vbNewLine
CBShipper.Items.Add(TextLine)
TextLine = ""
Loop


Else

MsgBox("File Does Not Exist")

End If
 
I've done some reading - someone has suggested I feed the reader into a TextBox first (a hidden textbox) and then into the Comobobox, but this doesn't seem necessary.
Is there a way I can simply knock off the last character displayed of every item in the Combobox, when it lists them in the drop down?
 
I've done some reading - someone has suggested I feed the reader into a TextBox first (a hidden textbox) and then into the Comobobox, but this doesn't seem necessary.

I suggest you never take advice from this person again. There is no reason to create a GUI control meant to display data to do simple string manipulation. Think of all the wasted overhead.


Is there a way I can simply knock off the last character displayed of every item in the Combobox, when it lists them in the drop down?
Well you could do as jmcilhinney suggests, for one. The advantage of using the IO.File.ReadAllLines method is that it handles the creation and disposal of the reader used to read the text file.

Also, note that you added the characters yourself with the end of this line of code: TextLine = TextLine & objReader.ReadLine() & vbNewLine

vbNewLine is adding a carriage return/line feed combination.

Also, there is also a handy method of the string class called String.Trim
 
Back
Top