3 Replies - 222 Views - Last Post: 07 February 2012 - 08:40 PM Rate Topic: -----

Topic Sponsor:

#1 eseyee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 12

Button click counting while selecting an item from a ListBox

Posted 07 February 2012 - 06:45 PM

The List Box has three candidates and a record Button. Every time the record button is hit I need it to add those button clicks for each candidate that is selected in the List Box. My code keeps counting all the clicks no matter which candidate I am selecting in the List Box. How can I differentiate between each selected item in the List Box.

Here is an image of how the application should look: http://i.imgur.com/N8zM2.jpg


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

Dim candidatevotes(2) As Integer
Dim vote
Dim total

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load

    candListBox.Items.Add("Mark Stone")
    candListBox.Items.Add("Sheima Patel")
    candListBox.Items.Add("Sam Perez")
    candListBox.SelectedIndex = 0

End Sub
Private Sub recordButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordButton.Click

    candidatevotes(vote) = candListBox.SelectedIndex

    total += candidatevotes(vote)


    Dim outfile As IO.StreamWriter
    outfile = IO.File.AppendText("voteinfo.txt")
    outfile.WriteLine(Convert.ToString(candListBox.SelectedItem))
    outfile.Close()

End Sub
Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click


    Dim infile As IO.StreamReader

    If IO.File.Exists("voteinfo.txt") = True Then
        infile = IO.File.OpenText("voteinfo.txt")

        infile.Close()
    End If


    markLabel.Text = total.ToString
    sheimaLabel.Text = total.ToString
    samLabel.Text = total.ToString


End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: Button click counting while selecting an item from a ListBox

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 162
  • View blog
  • Posts: 563
  • Joined: 03-February 10

Re: Button click counting while selecting an item from a ListBox

Posted 07 February 2012 - 06:57 PM

Based on your code - the candidate that you select in the listbox gives you the index of your candidatevotes, correct. So use that as the index to the array.

Now you know which candidate was selected you, you need to increment the value. So add one to it.
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • It's been so long, without this feeling
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,533
  • Joined: 24-July 11

Re: Button click counting while selecting an item from a ListBox

Posted 07 February 2012 - 07:25 PM

View Posteseyee, on 08 February 2012 - 11:45 AM, said:

The List Box has three candidates and a record Button. Every time the record button is hit I need it to add those button clicks for each candidate that is selected in the List Box. My code keeps counting all the clicks no matter which candidate I am selecting in the List Box. How can I differentiate between each selected item in the List Box.

Here is an image of how the application should look: http://i.imgur.com/N8zM2.jpg


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

Dim candidatevotes(2) As Integer
Dim vote
Dim total

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load

    candListBox.Items.Add("Mark Stone")
    candListBox.Items.Add("Sheima Patel")
    candListBox.Items.Add("Sam Perez")
    candListBox.SelectedIndex = 0

End Sub
Private Sub recordButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordButton.Click

    candidatevotes(vote) = candListBox.SelectedIndex

    total += candidatevotes(vote)


    Dim outfile As IO.StreamWriter
    outfile = IO.File.AppendText("voteinfo.txt")
    outfile.WriteLine(Convert.ToString(candListBox.SelectedItem))
    outfile.Close()

End Sub
Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click


    Dim infile As IO.StreamReader

    If IO.File.Exists("voteinfo.txt") = True Then
        infile = IO.File.OpenText("voteinfo.txt")

        infile.Close()
    End If


    markLabel.Text = total.ToString
    sheimaLabel.Text = total.ToString
    samLabel.Text = total.ToString


End Sub



I don't get it. You're opening the votes file, but you're not calling ReadLine from the StreamReader? Why open a file, and then not loop through it and read the contents?
Was This Post Helpful? 0
  • +
  • -

#4 eseyee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 12

Re: Button click counting while selecting an item from a ListBox

Posted 07 February 2012 - 08:40 PM

I fixed the problem


[code]
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Dim candidatevotes(2) As Integer
    Dim vote
    Dim total

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load

        candListBox.Items.Add("Mark Stone")
        candListBox.Items.Add("Sheima Patel")
        candListBox.Items.Add("Sam Perez")
        candListBox.SelectedIndex = 0

    End Sub
    Private Sub recordButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordButton.Click


        candidatevotes(candListBox.SelectedIndex) += 1




        Dim outfile As IO.StreamWriter
        outfile = IO.File.AppendText("voteinfo.txt")
        outfile.WriteLine(Convert.ToString(candListBox.SelectedItem))
        outfile.Close()

    End Sub
    Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click


        Dim infile As IO.StreamReader

        If IO.File.Exists("voteinfo.txt") = True Then
            infile = IO.File.OpenText("voteinfo.txt")

            infile.Close()
        End If


        markLabel.Text = candidatevotes(0).ToString
        sheimaLabel.Text = candidatevotes(1).ToString
        samLabel.Text = candidatevotes(2).ToString


    End Sub
End Class



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1