Solved

reading file as text

Posted on 2004-11-02
171 Views
Last Modified: 2010-05-02

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
0
Question by:FSA7
    5 Comments
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
     
    LVL 4

    Expert Comment

    by:cachedVB
    try running it in debug mode... the line 'Loop Until lineoftext Is Nothing' seems suspicious however
    0
     
    LVL 3

    Accepted Solution

    by:
    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
     

    Author Comment

    by:FSA7
    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
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: Foundations of Front-End Development

    Jump-start a lucrative career in front-end web development, with zero previous coding experience required. This course covers the basic programming concepts and languages required for creating engaging websites from scratch.

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now