reading file as text


Hello,

I am trying to write a program with textbox, button and listbox. The path is added to the textbox and when the button is clicked
it checks if the file exists, if yes, then it should read it as text with one string per line and place each string as a newitem in a listbox.

My program below seems to read the file, but then crashes, can you please advise what the problem is. Thanks


Private Sub cmdcheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdcheck.Click
        Dim sfiletocheck As String = Txtpath.Text
        If Not File.Exists(sfiletocheck) Then
            MessageBox.Show("Cannot find the file. Please try again !")
        Else
            Dim writefile As StreamReader
            Dim lineoftext As String

            writefile = File.OpenText(sfiletocheck)
            Do
                lineoftext = writefile.ReadLine
                lsttext.Items.Add(lineoftext)
            Loop Until lineoftext Is Nothing

        End If
    End Sub
FSA7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Try this out:

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Button1.Location = New System.Drawing.Point(144, 192)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(72, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Select File"
        '
        'ListBox1
        '
        Me.ListBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ListBox1.Location = New System.Drawing.Point(8, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(208, 173)
        Me.ListBox1.TabIndex = 2
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(224, 222)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim sr As StreamReader = New StreamReader(OpenFileDialog1.FileName)
            Dim inputLine As String
            ListBox1.BeginUpdate()
            ListBox1.Items.Clear()
            Do
                inputLine = sr.ReadLine
                If Not (inputLine Is Nothing) Then
                    ListBox1.Items.Add(inputLine)
                End If
            Loop While Not (inputLine Is Nothing)
            sr.Close()
            ListBox1.EndUpdate()
            Me.Text = OpenFileDialog1.FileName
        End If
    End Sub

End Class
0
cachedVBCommented:
try running it in debug mode... the line 'Loop Until lineoftext Is Nothing' seems suspicious however
0
sgayatriCommented:
This solves your problem. Kindly check the syntax of if - end if
I am not sure of this coding
Actuallly you were trying to write when eof is encountered.


             lineoftext = writefile.ReadLine

             if lineoftext is not Nothing then
             Do
               
                lsttext.Items.Add(lineoftext)
                lineoftext = writefile.ReadLine
            Loop Until lineoftext Is Nothing
            end if


This wi

       
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FSA7Author Commented:
Thanks everyone !
Sqayatri, your code is what i needed. it worked just fine.

the syntax of if statement should eb the following


            If lineoftext <> Nothing Then
                Do
                    Lsttext.Items.Add(lineoftext)
                    lineoftext = writefile.ReadLine
                Loop Until lineoftext Is Nothing
            End If
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you not try my code?  It works as well:

            Do
                inputLine = sr.ReadLine
                If Not (inputLine Is Nothing) Then
                    ListBox1.Items.Add(inputLine)
                End If
            Loop While Not (inputLine Is Nothing)

~IM
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.