Results 1 to 6 of 6

Thread: Remove Carriage Return Character from Combobox

  1. #1
    mattkw80 is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    49
    Reputation
    68

    Remove Carriage Return Character from Combobox

    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?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,346
    Reputation
    1543
    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.

  3. #3
    mattkw80 is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    49
    Reputation
    68
    '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

  4. #4
    mattkw80 is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    49
    Reputation
    68
    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?

  5. #5
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    Quote Originally Posted by mattkw80 View Post
    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.


    Quote Originally Posted by mattkw80 View Post
    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
    — Stephen Paszt

  6. #6
    mattkw80 is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    49
    Reputation
    68
    Figured out but thanks anyway guys.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking