Join 150,098 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,834 people online right now. Registration is fast and FREE... Join Now!
I'm doing a program that has 2 arrays, one with the names of states and the other with capitals. I also have a combobox with all the States names in it. The program is suppose to pop up a random capital in a label and then the user picks the state from the combo box and checks to see if the answer is correct. Im trying to assigned the picked item in the combo box to a variable to i can compare it the array. Here is what i have so far... Thanks
CODE
Option Explicit On Option Strict On Public Class Form1 ' puts the captials and states in a parallel array Dim States() As String = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"} Dim Capitals() As String = {"Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta", "Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Fills the Combo Box up with States StateComboBox.Items.Add("Alabama") StateComboBox.Items.Add("Alaska") StateComboBox.Items.Add("Arizona") StateComboBox.Items.Add("Arkansas") StateComboBox.Items.Add("California") StateComboBox.Items.Add("Colorado") StateComboBox.Items.Add("Connecticut") StateComboBox.Items.Add("Delaware") StateComboBox.Items.Add("Florida") StateComboBox.Items.Add("Georgia") StateComboBox.Items.Add("Hawaii") StateComboBox.Items.Add("Idaho") StateComboBox.Items.Add("Illinois") StateComboBox.Items.Add("Indiana") StateComboBox.Items.Add("Iowa") StateComboBox.Items.Add("Kansas") StateComboBox.Items.Add("Kentucky ") StateComboBox.Items.Add("Louisiana ") StateComboBox.Items.Add("Maine") StateComboBox.Items.Add("Maryland") StateComboBox.Items.Add("Massachusetts") StateComboBox.Items.Add("Michigan") StateComboBox.Items.Add("Minnesota") StateComboBox.Items.Add("Mississippi") StateComboBox.Items.Add("Missouri") StateComboBox.Items.Add("Montana") StateComboBox.Items.Add("Nebraska") StateComboBox.Items.Add("Nevada") StateComboBox.Items.Add("New Hampshire") StateComboBox.Items.Add("New Jersey") StateComboBox.Items.Add("New Mexico") StateComboBox.Items.Add("New York") StateComboBox.Items.Add("North Carolina") StateComboBox.Items.Add("North Dakota") StateComboBox.Items.Add("Ohio") StateComboBox.Items.Add("Oklahoma") StateComboBox.Items.Add("Oregon") StateComboBox.Items.Add("Pennsylvania") StateComboBox.Items.Add("Rhode Island") StateComboBox.Items.Add("South Carolina") StateComboBox.Items.Add("South Dakota") StateComboBox.Items.Add("Texas") StateComboBox.Items.Add("Utah") StateComboBox.Items.Add("Vermont") StateComboBox.Items.Add("Virginia") StateComboBox.Items.Add("Washington") StateComboBox.Items.Add("West Virginia") StateComboBox.Items.Add("Wisconsin") StateComboBox.Items.Add("Wyoming")
End Sub
Private Sub EvaluateChoice() 'Puts the the item that is selected into a variable Dim State As String = "" Dim Counter As Integer
'Compares the State Picked in combobox to the one in the array State = StateComboBox.SelectedItem
Do Until Counter = States.Length _ OrElse State = States(Counter) Counter = Counter + 1 Loop 'check to see what was found If Counter < States.Length Then State = States(Counter) Else End If
End Sub
Private Function GenerateRandomNumber() As Integer Dim rndRandom As New Random Dim intRandom As Integer intRandom = rndRandom.Next(49) lblCapital.Text = Capitals(intRandom)
End Function
Private Sub Resetinterface(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Call GenerateRandomNumber() End Sub End Class
You can simplify this a lot and make things much easier on yourself. Here is an example of how to set it up.
vb
Public Class Form1 ' Variables of the class should be declared with "Private" not "Dim" Private States() As String = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"} Private Capitals() As String = {"Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta", "Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"}
' Holds which capital number has been chosen Private chosencapital As Integer = 0
' No return value since you are not returning anything, you are setting values ' Set the capital chosen variable to the random number as well as the label Private Sub GenerateRandomNumber() Dim rndRandom As New Random
' Compare the selected index value (integer) against the number chosen Private Sub EvaluateChoice() If StateComboBox.SelectedIndex = chosencapital Then MessageBox.Show("You win!") Else MessageBox.Show("Try again!") End If
End Sub
' First of all lets load up the file listbox with some files from C:\Test that have the .csv extension. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Fills the Combo Box up with States ' Simple loop can do this, no need to repeat for each state. For i As Integer = 0 To 49 StateComboBox.Items.Add(States(i).ToString()) Next
End Sub
Private Sub btnGenerateCapital_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerateCapital.Click GenerateRandomNumber() End Sub
' Every time a combo item is checked, kick off the evaluation Private Sub StateComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StateComboBox.SelectedIndexChanged EvaluateChoice() End Sub End Class
As you can see here we simplified a lot of stuff. No need to add the states one by one because it is repetitive and can cause errors (like you missed Tennessee).
Secondly instead of trying to compare what is in the combo, only compare its position to that of the list. You can do this because your arrays are parallel. So when capital 3 was selected (and put into our form variable chosencapital) when they select the 4th state in the combo (with selectindex of 3) we compare it against the variable. If they match it means that the state they chose is the same as the chosencapital value.
Pressing our GenerateRandomNumber sets the capital text and the number chosen so call that wherever you like. Here we are calling it from a button called GenerateCapital.
Play around with it and see how it works. Enjoy!
"At DIC we be state and capital choosing code ninjas... I choose Olympia because its sexy!"