Need Help Writing to sequential text file

Send info to text file. Everytime a vote is added

Page 1 of 1

9 Replies - 4793 Views - Last Post: 01 December 2011 - 10:49 AM Rate Topic: -----

#1 visibasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-April 10

Need Help Writing to sequential text file

Posted 01 May 2010 - 10:17 AM

I am trying to create a program for a voters poll, where when a caller calls in, I can enter their vote for one of the four choices in the listbox and it will be automatically added to the vote count. I got that much to work, I need to save the vote to a sequential text file, when a vote is saved. I know I am close but it is just not quite working. I only need help getting the info to save to the sequential text file. I have also included a jpeg of what the form looks like.

Here's the code:




Option Strict On 
Option Explicit On 
  
Public Class MainForm 
  
    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
  
        Me.commercialListBox.Items.Add("Budweiser") 
        Me.commercialListBox.Items.Add("FedEx") 
        Me.commercialListBox.Items.Add("E*Trade") 
        Me.commercialListBox.Items.Add("Pepsi") 
  
    End Sub 
  
    Private Sub xSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xSave.Click 
  
        Const WKRKFile As String = "wkrk.txt" 
        Dim vote As Integer = Me.commercialListBox.SelectedIndex 
        Dim voteIndex As Integer 
        Static vBudweiser, vFedEx, vETrade, vPepsi As Integer 
  
        If My.Computer.FileSystem.FileExists(WKRKFile) Then 
            My.Computer.FileSystem.DeleteFile(WKRKFile) 
        End If 
  
        My.Computer.FileSystem.WriteAllText(WKRKFile, Me.commercialListBox.SelectedIndex.ToString, True) 
        'for x as Integer = 0   
        Select Case vote 
            Case 0 
                vBudweiser = vBudweiser + 1 
                My.Computer.FileSystem.WriteAllText(WKRKFile, Me.commercialListBox.SelectedIndex.ToString, True) 
            Case 1 
                vFedEx = vFedEx + 1 
                My.Computer.FileSystem.WriteAllText(WKRKFile, Me.commercialListBox.SelectedIndex.ToString, True) 
            Case 2 
                vETrade = vETrade + 1 
                My.Computer.FileSystem.WriteAllText(WKRKFile, Me.commercialListBox.SelectedIndex.ToString, True) 
            Case 3 
                vPepsi = vPepsi + 1 
                My.Computer.FileSystem.WriteAllText(WKRKFile, Me.commercialListBox.SelectedIndex.ToString, True) 
        End Select 
        'Next x 
        Me.xBudweiserBox.Text = vBudweiser.ToString 
        Me.xFedExBox.Text = vFedEx.ToString 
        Me.xeTrade.Text = vETrade.ToString 
        Me.xPepsiBox.Text = vPepsi.ToString 
  
    End Sub 
  
    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click 
        Me.Close() 
    End Sub 
End Class




Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help Writing to sequential text file

#2 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Need Help Writing to sequential text file

Posted 01 May 2010 - 12:13 PM

Hi,
what if you used the stream reader / stream writer, it's simple, try this out:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sw As New StreamWriter("c:\votes.txt", True)
        sw.WriteLine("any thing here")
        sw.Close()
    End Sub
End Class



the "True" parameter is to tell the stream writer to append to the file, so need to delete each time you wan to add something
also, if the file doesn't exist it will create it, so no need to check except for reading.
Was This Post Helpful? 0
  • +
  • -

#3 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Need Help Writing to sequential text file

Posted 28 November 2011 - 10:45 AM

I am working on this same project... Anyone Got the correct working version.

The book list the project like this:


Each year, WKRK-Radio polls its audience to determine the best Super Bowl commercial. Th e choices are as follows: Budweiser, FedEx, E*Trade, and Pepsi. The station manager wants an application that allows him to enter a callerís choice. The choice should be saved in a sequential access file. The application also should display the number of votes for each commercial. Create a Visual Basic Windows application. Use the following names for the solution, project, and form fi le, respectively: WKRK Solution, WKRK Project, and Main Form.vb. Create the interface shown in Figure 10-45, and then code the application. Save the solution and then start and test the application.

Must use stream writer.

The above code is not in the correct direction to work for this project. I'm lost. Please help...
Was This Post Helpful? 0
  • +
  • -

#4 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Need Help Writing to sequential text file

Posted 28 November 2011 - 08:09 PM

View Postxoptimizedrsx, on 28 November 2011 - 10:45 AM, said:

I am working on this same project... Anyone Got the correct working version.

The book list the project like this:


Each year, WKRK-Radio polls its audience to determine the best Super Bowl commercial. Th e choices are as follows: Budweiser, FedEx, E*Trade, and Pepsi. The station manager wants an application that allows him to enter a callerís choice. The choice should be saved in a sequential access file. The application also should display the number of votes for each commercial. Create a Visual Basic Windows application. Use the following names for the solution, project, and form fi le, respectively: WKRK Solution, WKRK Project, and Main Form.vb. Create the interface shown in Figure 10-45, and then code the application. Save the solution and then start and test the application.

Must use stream writer.

The above code is not in the correct direction to work for this project. I'm lost. Please help...


I made it this far but I cant get my boxes to count based on the words in the listbox1 for how many of each word is in that listbox1. I need my labels to show word count per work that's all now.


Option Explicit On
Option Strict On
Option Infer Off

Public Class Form1
    'Will use for count I think not sure at all...
    Dim bud As Integer
    Dim fed As Integer
    Dim trade As Integer
    Dim pep As Integer

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        lstCommercials.Items.Add("Budwiser")
        lstCommercials.Items.Add("FedEx")
        lstCommercials.Items.Add("E*Trade")
        lstCommercials.Items.Add("Pepsi")

    End Sub

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

        Me.Close()

    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim outfile As IO.StreamWriter

        outfile = IO.File.AppendText("WKRK.txt")
        outfile.WriteLine(lstCommercials.SelectedItem)
        outfile.Close()


    End Sub

    Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
        Dim inFile As IO.StreamReader
        Dim strName As String
        ListBox1.Items.Clear()
        If IO.File.Exists("WKRK.txt") Then

            inFile = IO.File.OpenText("WKRK.txt")

            Do Until inFile.Peek = -1

                strName = inFile.ReadLine
                ListBox1.Items.Add(strName)
            Loop
            inFile.Close()
        Else
            MessageBox.Show("Can't find the WKrk.txt file", _
                            "Commercials", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
        End If

        ' I need to make the count work here for the four bioxes based on my listbox1 I'm lost here...


    End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need Help Writing to sequential text file

Posted 28 November 2011 - 08:15 PM

I see a lot of instantiated objects folks, don't forget to dispose of those streamwriters not to mention make sure you use the new keyword to successfully instantiated your objects *cough* streamwriter *cough* to begin with. I'll add more and help later when I get to a computer, phone isn't helping.
Was This Post Helpful? 0
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need Help Writing to sequential text file

Posted 29 November 2011 - 05:34 AM

I edited a bit of your code, go place the path of your file in the places where it says *PATH* or *PATH OF FILE*. It should come out looking for example like "C:\TrevsFile\trev.txt". I don't understand what you mean by make the count work there.

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim outfile As IO.StreamWriter = new IO.StreamWriter("*PATH OF FILE*\wkrk.txt")
        Dim filestring as string = outfile.writeline(lstcommercials.selecteditem.tostring)
        'outfile = IO.File.AppendText("WKRK.txt")
        'outfile.WriteLine(lstCommercials.SelectedItem)
        outfile.Close()
        outfile.dispose

    End Sub

    Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
        Dim inFile As IO.StreamReader = new IO.StreamReader("*PATH OF FILE*\wkrk.txt")
        Dim strName As String
        ListBox1.Items.Clear()
        If IO.File.Exists("*PATH*\wkrk.txt") Then

            Do Until inFile.Peek = -1

                strName = inFile.ReadLine
                ListBox1.Items.Add(strName)

            Loop

            inFile.Close()
            infile.dipose

        Else

            MessageBox.Show("Can't find the WKrk.txt file", _
                            "Commercials", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
        End If

        ' I need to make the count work here for the four bioxes based on my listbox1 I'm lost here...


    End Sub

End Class


This post has been edited by trevster344: 29 November 2011 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Need Help Writing to sequential text file

Posted 29 November 2011 - 07:34 AM

View Posttrevster344, on 29 November 2011 - 05:34 AM, said:

I edited a bit of your code, go place the path of your file in the places where it says *PATH* or *PATH OF FILE*. It should come out looking for example like "C:\TrevsFile\trev.txt". I don't understand what you mean by make the count work there.

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim outfile As IO.StreamWriter = new IO.StreamWriter("*PATH OF FILE*\wkrk.txt")
        Dim filestring as string = outfile.writeline(lstcommercials.selecteditem.tostring)
        'outfile = IO.File.AppendText("WKRK.txt")
        'outfile.WriteLine(lstCommercials.SelectedItem)
        outfile.Close()
        outfile.dispose

    End Sub

    Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
        Dim inFile As IO.StreamReader = new IO.StreamReader("*PATH OF FILE*\wkrk.txt")
        Dim strName As String
        ListBox1.Items.Clear()
        If IO.File.Exists("*PATH*\wkrk.txt") Then

            Do Until inFile.Peek = -1

                strName = inFile.ReadLine
                ListBox1.Items.Add(strName)

            Loop

            inFile.Close()
            infile.dipose

        Else

            MessageBox.Show("Can't find the WKrk.txt file", _
                            "Commercials", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
        End If

        ' I need to make the count work here for the four bioxes based on my listbox1 I'm lost here...


    End Sub

End Class



First, thanks for the help.

up top the first guy put a screen shot up of the actual application.

we have 4 labels that are suppose to show how many of each word is in the read. such as if Budweiser is in it 8 times I get an 8 and so on for the other three names. Its a counter based on per name on how many times it shows up in the read function or listbox1.
Was This Post Helpful? 0
  • +
  • -

#8 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need Help Writing to sequential text file

Posted 30 November 2011 - 12:46 PM

Use a nested case statement in the peek loop. While the peek is -1, read the line, then use a case statement for the value of the string, if it contains one of the names add to your count for that particular label. You'll need to write this yourself.
Was This Post Helpful? 1
  • +
  • -

#9 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Need Help Writing to sequential text file

Posted 01 December 2011 - 04:17 AM

View Posttrevster344, on 30 November 2011 - 12:46 PM, said:

Use a nested case statement in the peek loop. While the peek is -1, read the line, then use a case statement for the value of the string, if it contains one of the names add to your count for that particular label. You'll need to write this yourself.


I got it to work now thanks. I was putting my counter data in the wrong location.
It would have been nice for the book to have had one example like this one so I would not have been lost as to where I needed to put the data.

I'll post the code later on what worked for me when I get back to that computer.
Was This Post Helpful? 0
  • +
  • -

#10 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Need Help Writing to sequential text file

Posted 01 December 2011 - 10:49 AM

View Postxoptimizedrsx, on 01 December 2011 - 04:17 AM, said:

View Posttrevster344, on 30 November 2011 - 12:46 PM, said:

Use a nested case statement in the peek loop. While the peek is -1, read the line, then use a case statement for the value of the string, if it contains one of the names add to your count for that particular label. You'll need to write this yourself.


I got it to work now thanks. I was putting my counter data in the wrong location.
It would have been nice for the book to have had one example like this one so I would not have been lost as to where I needed to put the data.

I'll post the code later on what worked for me when I get back to that computer.

This is the corrected code That follows the book without fancy steps.

Option Explicit On
Option Strict off
Option Infer Off

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        lstCommercials.Items.Add("Budwiser")
        lstCommercials.Items.Add("FedEx")
        lstCommercials.Items.Add("ETrade")
        lstCommercials.Items.Add("Pepsi")

    End Sub

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

        Me.Close()

    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim outfile As IO.StreamWriter

        outfile = IO.File.AppendText("WKRK.txt")
        outfile.WriteLine(lstCommercials.SelectedItem)
        outfile.Close()


    End Sub

    Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
        Dim inFile As IO.StreamReader
        Dim strName As String
        Dim bud As Integer
        Dim fed As Integer
        Dim trade As Integer
        Dim pep As Integer

        ListBox1.Items.Clear()
        If IO.File.Exists("WKRK.txt") Then
            inFile = IO.File.OpenText("WKRK.txt")

            

            Do Until inFile.Peek = -1
                strName = inFile.ReadLine
                If strName = "Budwiser" Then
                    bud = bud + 1
                ElseIf strName = "FedEx" Then
                    fed = fed + 1
                ElseIf strName = "ETrade" Then
                    trade = trade + 1
                ElseIf strName = "Pepsi" Then
                    pep = pep + 1
                End If

            Loop
            inFile.Close()


            lblbud.Text = bud.ToString
            lblFed.Text = fed.ToString
            lblTrade.Text = trade.ToString
            lblPepsi.Text = pep.ToString
        Else
            MessageBox.Show("I copied this from Online", _
                            "Stolen data", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
        End If


    End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1