none
VB.Net - Load listbox text from text file - Remove carriage return symbol RRS feed

  • Question

  • I have a text file that loads its text into a listbox, which I then use to complete various tasks in my application such as populating docvariable fields and completing cells in an excel spreadsheet.  I have one really annoying issue with my code below.  The listbox has a [] symbol beside every item.  Also, when text is populated into the docvariables or excel spreadsheet it gives me the text contained in the listbox selected item, but it also gives me an extra carriage return.  Is there any way to modify my code to remove that pesky carriage return?

    Public Class MainForm
    Private path as String = “C:\Folder\”
    Private Sub Button1_Click(ByVal sender As Systen.Object, ByVal e As System.EventArgs) Handles Button1.Blick
    Dim a As String = My.Computer.FileSystem.ReadAllText(path & “ItemInfo.txt”)
    Dim b As String() = a.Split(vbNewLine)
    itemListBox.Items.AddRange(b)
    End Sub

    Any help would be appreciated.  Thank you!
    Thursday, September 6, 2012 5:39 PM

Answers

  • I just found a better way to go about my issue:

    Public Class Form1
        Dim R As IO.StreamReader
    
        Private Sub NameCB_Enter(sender As Object, e As EventArgs) Handles NameCB.Enter
            R = New IO.StreamReader("C:\File.txt")
            While (R.Peek() > -1)
                NameCB.Items.Add(R.ReadLine)
            End While
            R.Close()
        End Sub
    
    Hope this helps someone else too.
    • Marked as answer by MBuchok Thursday, September 6, 2012 10:46 PM
    Thursday, September 6, 2012 10:46 PM

All replies

  • I just found a better way to go about my issue:

    Public Class Form1
        Dim R As IO.StreamReader
    
        Private Sub NameCB_Enter(sender As Object, e As EventArgs) Handles NameCB.Enter
            R = New IO.StreamReader("C:\File.txt")
            While (R.Peek() > -1)
                NameCB.Items.Add(R.ReadLine)
            End While
            R.Close()
        End Sub
    
    Hope this helps someone else too.
    • Marked as answer by MBuchok Thursday, September 6, 2012 10:46 PM
    Thursday, September 6, 2012 10:46 PM
  • Hi LAssist2012,

    I'm glad to hear that you have solved your issue. Thanks for sharing your experience here. It's very beneficial for other community members who have similar issue to see how you solved yours.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, September 7, 2012 10:05 AM
    Moderator