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

Visual Basic.NET

Avatar of undefined
Last Comment
Nasir Razzaq

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Nasir Razzaq

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Éric Moreau

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!
anault24

ASKER
I'm not writing a book, I am trying to learn some vb coding from this book. @EricMoreau
Éric Moreau

my apologies, I misread your sentence. I thought that "I am working on an excercise" on meant writing it.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
anault24

ASKER
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
Nasir Razzaq

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.