i want to users to be able to vote for their favourite soft drink. They will input the names of the soft drinks through the use of an inputbox, the names input are stored in an array.

what i would like to do is count the votes given to each soft drink. How would this be done, please provide example code if possible. thanks

p.s. i tried using a listbox and command button but have failed.

Here is my code;

[U] Global Variables[/U]
Dim candidatearray() As String
    Dim drink1 As Integer
    Dim drink2 As Integer
    Dim drink3 As Integer
    Dim drink4 As Integer
    Dim drink5 As Integer

[U]Code; [/U]   Private Sub btnsetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsetup.Click

noofdrinks = InputBox("Please enter the number of drinks", "number of drinks")

        Do While noofdrinks > 5 Or noofdrinks < 2
            MsgBox("Please enter a vaild number of drinks, between 2 and 5")
            noofdrinks = InputBox("Please enter the number of drinks", "noofdrinks")
        Loop

        Dim X As Integer = 0
        drinknumber = 0

        ReDim drinkarray(noofcandidates)

        Do Until noofdrinks = drinknumber

           drinkname = InputBox("Enter the drinks name:", "drinkname")
            drinknumber = drinknumber + 1

            lbxvotedrinks.Items.Insert(X, drinkname)
            X = X + 1
 
            candidatearray(i) = candidatename
            i = i + 1

 Private Sub btnvotefordrink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnvoteforcan.Click

        If lbxvotedrinks.SelectedItem = 0 Then
            drink1 = drink1 + 1
            votesballot = votesballot + 1
        End If

        If lbxvotecdrinks.SelectedItem = 1 Then
            drink2 = cdrink2 + 1
            votesballot = votesballot + 1
        End If

        If lbxvotedrinks.SelectedItem = 2 Then
            drink3 = drink3 + 1
            votesballot = votesballot + 1
        End If

        If lbxvotedrinks.SelectedItem = 3 Then
          drink4 = drink4 + 1
            votesballot = votesballot + 1
        End If

        If lbxvotedrinks.SelectedItem = 4 Then
            drink5 = drink5 + 1
            votesballot = votesballot + 1
        End If

        If lbxvotedrinks.SelectedItem = 5 Then
            drink1 = drink1 + 1
            votesballot = votesballot + 1
        End If

    End Sub

Could you sugget other ways of storing the votes separately for each candidate. thanks

First, i would make a two arrays, one fore the name and one for the rating. Then i would use the for loop to get the names then get the rating for those names. Finaly i would display those inputs in a text box.

Here it is:

Dim favSoftDrink() As String
    Dim favSoftDrinkRating() As Integer
    Dim results As String
    Dim b As Integer
    Dim a As Integer
    Dim NumDrinks As Integer
    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click

        NumDrinks = InputBox("How many Soft drink are you going to vote on?")
        ReDim favSoftDrink(NumDrinks)
        ReDim favSoftDrinkRating(NumDrinks)
        For b = 0 To NumDrinks - 1
            favSoftDrink(b) = InputBox("Please Enter The Name of the soft drink number:" & b + 1)
        Next
        For a = 0 To NumDrinks - 1
            favSoftDrinkRating(a) = InputBox("Please Enter a rating for " & favSoftDrink(a))
        Next
        For c = 0 To NumDrinks - 1
            results += "SoftDrink: " & favSoftDrink(c) & vbTab & "Rating: " & favSoftDrinkRating(c) & vbCrLf
        Next
        TextBox1.Text = results
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.