I always have a problem with this. I get an error when trying to build the program "argument exception unhandled". Any help would be appreciated, thanks.

Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click
        Dim newLineIndex As Integer = 0

        If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
            Do Until newLineIndex = -1
                lstdisplay.Items.Add(txtinput.Text)

            Loop
        End If
    End Sub

Recommended Answers

All 9 Replies

Not really sure what you're trying to do, but to add a string from the textbox to the list it's as simple as: lstdisplay.Items.Add(txtinput.Text) Are you trying to add the contents of the text file into a listbox? Also what are you trying to do with the loop?

yes, I'm trying to append contents of the textbox to the end of the listbox. The loop isn't correct I removed it already, I just need to get the contents of the textbox to append to the end of the listbox. Here is the complete code

Public Class Form1
    Private path As String = "E:\Visual Basic\"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstdisplay.Items.Clear()
        My.Computer.FileSystem.OpenTextFileReader(path & "stocks.txt")
        lstdisplay.DataSource = IO.File.ReadAllLines(path & "stocks.txt")
    End Sub

    Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click
        Me.Close()
    End Sub
    'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter
    '   txtinput.SelectAll()
    'End Sub
    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click
        'Dim newLineIndex As Integer = 0

        'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
        'Do Until newLineIndex = -1
        lstdisplay.Items.Add(txtinput.Text)
        txtinput.Focus()
        '   Loop
        'End If
    End Sub



End Class

any thoughts on this one?

Will this help?

Dim zz As String = My.Computer.FileSystem.ReadAllText(path & "stocks.txt")
        Dim a As String() = Split(zz, vbNewLine)
        ListBox1.Items.AddRange(a)

no still didn't work, I'm stumped I've tried everything. Cannot add items to lstdisplay (listbox). Maybe because the listbox is being populated by a file?

The reason the code doesn't work is because adding an item to a listbox using the .items.add is different than setting the datasource. They don't work together.

If you want to continue to use the datasource (which I personally don't recommend) you can try this out. Use a variable for the datasource and append the txtInput to it and reset the datasource of the list.

Private path As String = "D:\Mark\"
    Dim datasource As New List(Of String)


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstFile.Items.Clear()
        My.Computer.FileSystem.OpenTextFileReader(path & "ITS_Stuff.txt")

        Dim a() As String = IO.File.ReadAllLines(path & "ITS_Stuff.txt")
        For Each content As String In a
            datasource.Add(content)
        Next
        lstFile.DataSource = datasource
    End Sub


    Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter
    '   txtinput.SelectAll()
    'End Sub
    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'Dim newLineIndex As Integer = 0

        'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
        'Do Until newLineIndex = -1
        datasource.Add(txtInput.Text())
        lstFile.DataSource = Nothing
        lstFile.DataSource = datasource
        lstFile.Refresh()

        txtinput.Focus()
        '   Loop
        'End If
    End Sub

A simpler way to do it is just to use the .items.add for the contents of the file and the txtInput

Private path As String = "D:\Mark\"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstFile.Items.Clear()
        My.Computer.FileSystem.OpenTextFileReader(path & "ITS_Stuff.txt")

        Dim a() As String = IO.File.ReadAllLines(path & "ITS_Stuff.txt")
        For Each content As String In a
            lstFile.Items.Add(content)
        Next

    End Sub


    Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter
    '   txtinput.SelectAll()
    'End Sub
    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'Dim newLineIndex As Integer = 0

        'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
        'Do Until newLineIndex = -1
        lstFile.Items.Add(txtInput.Text)

        txtinput.Focus()
        '   Loop
        'End If
    End Sub
commented: a great help thanks +2

thanks, now I can add items in the listbox. I also need to remove items when selected this is the code I have now but, of course it's not working, this is the code that I have so far

Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click
        If lstdisplay.SelectedIndex > -1 Then
            lstdisplay.Items.RemoveAt(lstdisplay.SelectedIndex)

        End If
    End Sub

would I have to change the lstdisplay.Items.RemoveAt(lstdisplay.SelectedIndex) line to something like lstdisplay.datasource.????

If you are going to use the datasource approach, you'll have to modify the datasource variable and reset the datasource of the list.

Private path As String = "c:\"
    Dim datasource As New List(Of String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstFile.Items.Clear()
        Dim a() As String = IO.File.ReadAllLines(path & "dschat.log")

'I prefer to work with collections rather than arrays in .net so I'll move the array to a strongly typed List(Of ) collection
        For Each content As String In a
            datasource.Add(content)
        Next
        lstFile.DataSource = datasource
    End Sub

    ''' <summary>
    ''' Use this to refresh the list datasource.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetDatasource()
        lstFile.DataSource = Nothing
        lstFile.DataSource = datasource
        lstFile.Refresh()
    End Sub

    Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click

        If lstFile.SelectedIndex > -1 Then
            datasource.RemoveAt(lstFile.SelectedIndex)
            SetDatasource()
        End If

    End Sub

    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        datasource.Add(txtInput.Text())
        SetDatasource()
        txtInput.Focus()
    End Sub

Yes, thank you everything seems to be working beautifully. I added this to the exit button to write added content back to file.

Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click
        If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
            My.Computer.FileSystem.WriteAllText(path & "stocks.txt", String.Empty, False)
        End If
        'loop to enter all data in list box into the file
        For Each item As String In lstdisplay.Items
            My.Computer.FileSystem.WriteAllText(path & "stocks.txt", item & ControlChars.NewLine, True)
        Next
        Me.Close()
    End Sub

and it actually works !!!

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.