If someone can tell me what I'm doing wrong I would greatly appreciate it. I have a listbox of all the US states and an array of the capital of all the states. What I'm trying to do is when the state is selected and the button is clicked, then it will output the capital of state in a textbox. This is my code:

Public Class Form1

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim str1 As String

    Dim CapitalsArray() 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"}

    str1 = " is the capital of the state "

    Dim stateArray(50) As String
    Dim i As Integer

    For i = 0 To lstStates.Items.Count - 1
        stateArray(i) = lstStates.Items(i)
    Next

    If lstStates.SelectedItem = i Then
        stateArray(i) = CapitalsArray(i)
    End If

    txtOutput.Text = CapitalsArray(i) & str1 & stateArray(i)

End Sub

Store your data in a dictionary as follows:

Private Capitals As New Dictionary(Of String, String) From {
    {"Manitoba", "Winnipeg"},
    {"Saskatchewan", "Regina"},
    {"Ontario", "Toronto"}
}

You can populate the combobox by

For Each province As String In Capitals.Keys
    ComboBox1.Items.Add(province)
Next

and when the user clicks the button you can do

If ComboBox1.SelectedIndex >= 0 Then
    Dim province As String = ComboBox1.Text
    MsgBox("The capital of " & province & " is " & Capitals(province))
End If

Just substitute "state" for "province".

Thanks for the input, but I'm not quite understanding your steps. It seems as if what you are saying is I add each to a combobox? I all 50 US states in a listbox and I had that capital array in attempt to output the corresponding capital when the state is selected. So the states are already there, so I'm not understanding why to populate a combobox. Can you clarify that to me please?

You start with the dictionary. The dictionary uses the state name as the key. The value of the entry for that key is the state capital. You can initialize that in one statement as I showed you. In the form load handler you can iterate through the keys (again, as I showed you) to populate the combobox.

In the button click event you first check to make sure a state has been selected. If so, then you get the capital by indexing the dictionary with the selected state name. If, instead, you want to use a listbox then populate the listbox on form load instead of the combobox. Same logic.

I apologize for confusing you by using combobox instead of listbox. I'm in the middle of several things at the moment and I got a little off track.

This article has been dead for over six months. Start a new discussion instead.