Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

reading file as text

Posted on 2004-11-02
5
Medium Priority
?
209 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
Comment
Question by:FSA7
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12475564
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
ID: 12477174
try running it in debug mode... the line 'Loop Until lineoftext Is Nothing' seems suspicious however
0
 
LVL 3

Accepted Solution

by:
sgayatri earned 200 total points
ID: 12480610
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
ID: 12481542
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 86

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

916 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