Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:52:43+00:00 2026-06-02T18:52:43+00:00

I am working on a program in Visual Basic that incorporates using a sequential

  • 0

I am working on a program in Visual Basic that incorporates using a sequential access file for a ballot type program. Each type the user clicks the save vote button, the amount of votes gets stored. What I am trying to do is in my access file is to display the number of votes as an actual number. The way my program is written right now, the name of the candidate appears as many times as the vote was saved. for example, if perez was voted for 4 times, in the access file perez is displayed on 4 different lines. How do I display the actual number of how many time they were voted for. I’m thinking using a counter variable, but not really sure how to really implement it in. this is what i have so far.

Public Class Voter

Dim file As IO.File

Dim infile As IO.StreamReader

Dim outfile As IO.StreamWriter



Private Sub Voter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    outfile = IO.File.CreateText("Votes.txt")
End Sub

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
    lstResult.Items.Clear()
    'which buttons is clicked
    If radMark.Checked = True Then
        outfile.WriteLine(radMark.Text)
        radMark.Checked = False
    ElseIf radSheima.Checked = True Then
        outfile.WriteLine(radSheima.Text)
        radSheima.Checked = False
    ElseIf radSam.Checked = True Then
        outfile.WriteLine(radSam.Text)
        radSam.Checked = False
    Else
        MessageBox.Show("You should select one among them")
    End If
End Sub

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
    'Dim Mark, Sheima, Sam As Integer
    Dim Mark As Integer = 0
    Dim Sheima As Integer = 0
    Dim Sam As Integer = 0
    Dim name As String
    'Mark = Sheima = Sam = 0
    outfile.Close()
    infile = IO.File.OpenText("Votes.txt")
    'keep track of votes
    While Not infile.EndOfStream
        name = infile.ReadLine()
        If name.Equals("Mark Stone") Then
            Mark += 1
        ElseIf name.Equals("Sheima Patel") Then
            Sheima += 1
        Else
            Sam += 1
        End If
    End While
    'results
    lstResult.Items.Clear()
    lstResult.Items.Add("Mark Stone     " & CStr(Mark))
    lstResult.Items.Add("Shemia Patel   " & CStr(Sheima))
    lstResult.Items.Add("Sam Perez      " & CStr(Sam))
    infile.Close()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Me.Close()

End Sub
End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:52:48+00:00Added an answer on June 2, 2026 at 6:52 pm

    In your btnVote_Click function you are writing the radiobutton text into the file everytime you click on it, that’s how the issue to be created.

    Also you are counting how many times the name appears in the file as the number of vote but not the number.

    You should try putting the name and count in your vote file rather than just the name, e.g.

    Mark,1
    Sheima,2
    Same,5

    Then when you click Vote button, you should read the number for Mark, increment by 1 and write it back.

    Try this,

    Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
        lstResult.Items.Clear()
        'which buttons is clicked
        If radMark.Checked = True Then
            AddCount(radMark.Text)
            radMark.Checked = False
        ElseIf radSheima.Checked = True Then
            AddCount(radSheima.Text)
            radSheima.Checked = False
        ElseIf radSam.Checked = True Then
            AddCount(radSam.Text)
            radSam.Checked = False
        Else
            MessageBox.Show("You should select one among them")
        End If
    End Sub
    
    Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
    
        'results
        lstResult.Items.Clear()
        lstResult.Items.Add("Mark Stone     " & GetCount(radMark.Text))
        lstResult.Items.Add("Shemia Patel   " & CStr(radSheima.Text))
        lstResult.Items.Add("Sam Perez      " & CStr(radSam.Text))
    
    End Sub
    
    Private Sub AddCount(Byval VoteName As String)
    
        infile = New IO.File.StreamReader("Votes.txt")
    
        Dim whole_line As String
        Dim person As String
        Dim vote_count As Integer
        Dim found as boolean
    
        'read through the whole file until the end or find the name
        Do While infile.Peek() >= 0 And person <> VoteName
    
            'read each line
            whole_line = infile.ReadLine
    
            person = Split(whole_line, ",")(0)
    
            If person = VoteName Then
    
                vote_count = Split(whole_line, ",")(1)
    
    
                found = True
            End If
        Loop
    
        'Close the file after it is used.
        infile.Close()
    
    
        'Reopen the file with the StreamWriter
        outfile = IO.File.OpenText("Votes.txt")
    
        If found = True Then
            'the line will only be replace if the person name is found in the line
            whole_line = Whole_line.replace(VoteName & "," & vote_count, VoteName & "," & vote_count + 1)
    
            'Write back into the file
            outfile.WriteLine(whole_line)
        Else
            'if the vote name is not found in the file
            'Then create a new one
            outfile.WriteLine(VoteName & ",1" & VbCrLf)
        End If
    
            outfile.Close()
    
    End Sub
    
    Private Function GetCount(Byval VoteName As String)
        infile = New IO.File.StreamReader("Votes.txt")      
    
        Dim whole_line As String
        Dim person As String
        Dim vote_count As Integer
    
        'read through the whole file
        Do While infile.Peek() >= 0 And person <> VoteName
    
            'read each line
            whole_line = infile.ReadLine
    
            person = Split(Whole_line, ",")(0)
    
            If person = VoteName Then
                vote_count = Split(whole_line, ",")(1)
            End If
        Loop
    
        infile.Close()
        Return vote_count
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I working working with Visual Basic 2010 Express. I have a DataGridView that is
I've been trying to get to get a basic v8 program working under visual
I've been working on a simple windows program using Visual C++ 2010 express on
I'm working on a quick setup program in Visual Studio and wanted to change
I'm working on a C# windows program with Visual Studio 2008. Usually, I work
I have a working program in C++ that generates data for a Mandelbrot Set.
I'm trying to get a helloworld type program working with REST CI/jquery. I've included
Am working on a program that will allow a graph of nodes to be
I am working on a pretty basic C# visual studio forms application but am
I want to get started using Boost. I'm programming a C++ program in Visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.