Welcome to Dream.In.Code
Become a VB Expert!

Join 137,262 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,507 people online right now. Registration is fast and FREE... Join Now!




Trouble with reading and writing to a document

 
Reply to this topicStart new topic

Trouble with reading and writing to a document

KiKaL
4 Dec, 2006 - 02:57 PM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2002
Posts: 31

CODE
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim sr As System.IO.StreamWriter
        Dim k As Integer
        If MessageBox.Show("Want to quit?", "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        Else
            sr = IO.File.AppendText("info.txt")
            For k = 0 To gIndex
                sr.WriteLine(gStudentArray(k).Name & vbTab & gStudentArray(k).Department & vbTab & gStudentArray(k).Score.ToString & vbTab & gStudentArray(k).Gender & vbTab & gStudentArray(k).Deans)
            Next
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sr As System.IO.StreamReader
        Dim Line As String

        If System.IO.File.Exists("info.txt") = False Then
            MessageBox.Show(" There is no input file.")
            Exit Sub
        End If
        sr = System.IO.File.OpenText("info.txt")
        Do Until sr.Peek = -1
            Line = sr.ReadLine()
            lstStudents.Items.Add(Line)
            gIndex = gIndex + 1
            ReDim Preserve gStudentArray(gIndex)
        Loop
        sr.Close()
    End Sub




Alright so what im trying to do is have the program read infromation from a text file called info.txt or create it if its not already there. If there is information in the file I want it to be displayed in the listbox lstStudents.

When the file is closed, I want it to ask the user if they want to quit and then if they say yes have all the information saved to the Info.txt file.

I feel like for the most part my code is right but Im still having trouble totally grasping the concept of writting and retrieving info.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Trouble With Reading And Writing To A Document
4 Dec, 2006 - 03:03 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
What specifically are you having trouble with?

Does your code not work the way you expected or are you getting any error messages?

User is offlineProfile CardPM
+Quote Post

KiKaL
RE: Trouble With Reading And Writing To A Document
4 Dec, 2006 - 05:25 PM
Post #3

New D.I.C Head
*

Joined: 9 Oct, 2002
Posts: 31

QUOTE(jayman9 @ 4 Dec, 2006 - 04:03 PM) *

What specifically are you having trouble with?

Does your code not work the way you expected or are you getting any error messages?



~edit I think I got it working

This post has been edited by KiKaL: 4 Dec, 2006 - 06:14 PM


Attached File(s)
Attached File  lab10.zip ( 78.94k ) Number of downloads: 32
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 07:12PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month