Coding Sequential access file- Visual Basic Code

ADMINISTRATOR: THIS IS NOT FOR AN ACADEMIC NOR SCHOOL RELATED ASSIGNMENT

Hey experts,

 I am working on an excercise in my 2008 visual basic book, and for my program I am trying to code the Save button to write the gross pay amount to a sequential access file named gross.txt. I also am trying to code the Display button to read the gross pay amounts from the gross.txt file and display each (formatted with a dollar sign and two decimal places) in the list box? Can anyone give me some help?

Thank You


Code:

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtGrossPay_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGrossPay.KeyPress
        ' allows the text box to accept only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click




    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click



    End Sub
End Class

Open in new window

anault24Asked:
Who is Participating?
 
CodeCruiserCommented:
For saving, check out the AppendAllLines or AppendText methods of the System.IO.File class

https://msdn.microsoft.com/en-us/library/system.io.file%28v=vs.110%29.aspx


For reading, try ReadAllLines or ReadAllText methods of the System.IO.File class.
0
 
Éric MoreauSenior .Net ConsultantCommented:
Why would somebody write a book for VB 2008 when 2015 is soon to be released? And normally, when somebody is writing a book, they master simple stuff like this!
0
 
anault24Author Commented:
I'm not writing a book, I am trying to learn some vb coding from this book. @EricMoreau
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Éric MoreauSenior .Net ConsultantCommented:
my apologies, I misread your sentence. I thought that "I am working on an excercise" on meant writing it.
0
 
anault24Author Commented:
Can't seem to format txtGrossPay.text to format a dollar sign and two decimal places when a number is entered?






Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtGrossPay_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGrossPay.KeyPress
        ' allows the text box to accept only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ' saves a gross pay amount to a sequential access file




        ' declare a StreamWriter variable
        Dim outfile As IO.StreamWriter


        'open the file for append
        outfile = IO.File.AppendText("gross.txt")

        ' write the name on a seperate line in the file
        outfile.WriteLine(txtGrossPay.Text)

        ' close the file
        outfile.Close()

        ' clear the list box, then set focus
        lstContents.Items.Clear()
        txtGrossPay.Focus()
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' reads gross pay amount from a sequential access file
        ' and displays them in the list

        ' declare variables
        Dim inFile As IO.StreamReader
        Dim strGrossPay As String

        'determine whether file exists
        If IO.File.Exists("gross.txt") Then

            ' open file for input
            inFile = IO.File.OpenText("gross.txt")

            'process loop instructions until end of file
            Do Until inFile.Peek = -1
                strGrossPay = inFile.ReadLine

                'add gross pay amount to list box
                lstContents.Items.Add(strGrossPay)



            Loop

            'close the file
            inFile.Close()
        Else
            MessageBox.Show("Can't seem to find her for you sir!")
        End If
    End Sub
End Class

Open in new window

0
 
CodeCruiserCommented:
Do you want to format it in textbox or in file?

Use String.Format

https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx
0
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.

All Courses

From novice to tech pro — start learning today.