943,745 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 465
  • VB.NET RSS
Feb 18th, 2009
0

How to simulate voting vb.net ?

Expand Post »
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;
      Global Variables
Dim candidatearray() As String
    Dim drink1 As Integer
    Dim drink2 As Integer
    Dim drink3 As Integer
    Dim drink4 As Integer
    Dim drink5 As Integer

Code;    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
Last edited by asif786; Feb 18th, 2009 at 6:30 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asif786 is offline Offline
19 posts
since Dec 2008
Feb 18th, 2009
0

Re: How to simulate voting vb.net ?

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:
vb Syntax (Toggle Plain Text)
  1. Dim favSoftDrink() As String
  2. Dim favSoftDrinkRating() As Integer
  3. Dim results As String
  4. Dim b As Integer
  5. Dim a As Integer
  6. Dim NumDrinks As Integer
  7. Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
  8.  
  9. NumDrinks = InputBox("How many Soft drink are you going to vote on?")
  10. ReDim favSoftDrink(NumDrinks)
  11. ReDim favSoftDrinkRating(NumDrinks)
  12. For b = 0 To NumDrinks - 1
  13. favSoftDrink(b) = InputBox("Please Enter The Name of the soft drink number:" & b + 1)
  14. Next
  15. For a = 0 To NumDrinks - 1
  16. favSoftDrinkRating(a) = InputBox("Please Enter a rating for " & favSoftDrink(a))
  17. Next
  18. For c = 0 To NumDrinks - 1
  19. results += "SoftDrink: " & favSoftDrink(c) & vbTab & "Rating: " & favSoftDrinkRating(c) & vbCrLf
  20. Next
  21. TextBox1.Text = results
  22. End Sub
Reputation Points: 10
Solved Threads: 8
Junior Poster
toko is offline Offline
104 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: One form data to be inserted/updated into multiple tables
Next Thread in VB.NET Forum Timeline: Linking Comboboxes in VB 2008





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC