Link to home
Start Free TrialLog in
Avatar of anault24
anault24

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
Avatar of anault24
anault24

ASKER

I'm not writing a book, I am trying to learn some vb coding from this book. @EricMoreau
my apologies, I misread your sentence. I thought that "I am working on an excercise" on meant writing it.
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial