Hi, I am trying to store individual lines of a .txt file in a ComboBox, here is my code:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim FILE_NAME As String = "history.txt"

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

            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            Dim Line As String = ""

            Do While objReader.Peek() <> -1
                Line = Line & objReader.ReadLine() & vbNewLine
                ComboBox1.Items.Add(Line)
            Loop

        Else

            'MsgBox("File Does Not Exist")

        End If
    End Sub

Whenever I run this, I get the TargetInvocationException error. I do not know how to fix this, after many tries. Anyone know how to fix this?

Recommended Answers

All 6 Replies

Well your code is a bit off, Line = Line & objReader.ReadLine() & vbNewLine Remove the line after = and the vbNewLine. The combobox will then have an entry for every line in the .txt file.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FILE_NAME As String = "c:\file.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            Dim Line As String = ""
            Do While objReader.Peek() <> -1
                Line = objReader.ReadLine()
                ComboBox1.Items.Add(Line)
            Loop
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub

I am not sure about the error. Are you using VB.NET Express 2012? I tried it and it worked fine. What line is the error occurring?

I am using VS Ultimate 2012 and there is no line specified for where the error is occuring.

Is this an actual error or just a warning? Try this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim FILE_NAME As String = "c:\file.txt"
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
                Dim Line As String = ""
                Do While objReader.Peek() <> -1
                    Line = objReader.ReadLine()
                    ComboBox1.Items.Add(Line)
                Loop
            Else
                MsgBox("File Does Not Exist")
            End If
        Catch ex As Exception
            msgbox(ex.message)
        End Try
End Sub

Could you post the message it gives you, if any.

Edit: I think you may not have permission to view the file.

Does the error occur when you debug? What did the messagebox say? Try Turning the error off in the property menu. Is the location of the file accessible?

I got it to work. The program would crash if I put it in the Form1_Load sub. I found a workaround for it. Thanks for helping me.

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.