none
Help with Visual Basic code? RRS feed

  • Question

  • So for one of my assignments I need to make two listboxes, one with a list of cities and one where depending what city is selected will show the zipcode for that city. There is no display button, it should automatically show the zipcode(s). 

    I have the first part done: 

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load 
    lstCities.Items.Add("Park Ridge") 
    lstCities.Items.Add("Barrington") 
    lstCities.Items.Add("Glen Ellyn") 
    lstCities.Items.Add("Algonquin") 
    lstCities.Items.Add("Crystal Lake") 
    lstCities.SelectedIndex = 0 
    End Sub 

    But when I try to do the second part, the zipcode listbox is always empty. Help?!?! 

    Private Sub lstCities_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstCities.SelectedValueChanged 
    Dim strCity As String 
    strCity = lstCities.SelectedValue 
    Select Case strCity 
    Case "Park Ridge" 
    ListBox1.Text = "60068" 
    Case "Barrington" 
    ListBox1.Text = "60010" & "60011" 
    Case "Glen Ellyn" 
    ListBox1.Text = "60137" & "60138" 
    Case "Algonquin" 
    ListBox1.Text = "60102" 
    Case "Crystal Lake" 
    ListBox1.Text = "60012" 
    End Select 
    End Sub 
    End Class

    Here is what my exact assignment says:

     Code the application. The form’s Load event procedure should add the city names
    shown in Figure 6-63 to the list box and then select the first name in the list. The list
    box’s SelectedValueChanged event procedure should assign the item selected in the
    list box to a variable. It then should use the Select Case statement to display the
    city’s ZIP code(s).

    Wednesday, October 2, 2013 3:47 PM

Answers

All replies

  • "But when I try to do the second part, the zipcode listbox is always empty."

    You don't fill the listbox, hence it's empty. You only set it's Text property. Look at the remarks section to see what setting the property does: http://msdn.microsoft.com/en-us/library/System.Windows.Forms.ListBox.Text


    Armin

    Wednesday, October 2, 2013 4:26 PM
  • The selectedvalue is used when the listbox is bound. 

    That is not in your case you simply would use the selecteditem

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem.aspx

     And use then of course the selectedindexchanged event

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged.aspx


    Success
    Cor


    Wednesday, October 2, 2013 4:30 PM
  • Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            lstCities.DropDownStyle = ComboBoxStyle.DropDownList
            lstCities.Items.Add("Park Ridge")
            lstCities.Items.Add("Barrington")
            lstCities.Items.Add("Glen Ellyn")
            lstCities.Items.Add("Algonquin")
            lstCities.Items.Add("Crystal Lake")
            lstCities.SelectedIndex = 0
    
    
            ListBox1.Items.Add("60068")
            ListBox1.Items.Add("60010" & "60011")
            ListBox1.Items.Add("60137" & "60138")
            ListBox1.Items.Add("60102")
            ListBox1.Items.Add("60012")
        End Sub
    
    
        Private Sub lstCities_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCities.SelectedValueChanged
            Dim strCity As String
            strCity = lstCities.Text
            Select Case strCity
                Case "Park Ridge"
                    ListBox1.Text = "60068"
                Case "Barrington"
                    ListBox1.Text = "60010" & "60011"
                Case "Glen Ellyn"
                    ListBox1.Text = "60137" & "60138"
                Case "Algonquin"
                    ListBox1.Text = "60102"
                Case "Crystal Lake"
                    ListBox1.Text = "60012"
            End Select
        End Sub


    Wednesday, October 2, 2013 4:39 PM
  • A datatable is in my idea the most easy way to fulfill what you want to do, look at this sample on our website.

    http://www.vb-tips.com/RelatedListBoxWithDataView.ASPX

    I was a little bit lazy, you can use of course as well to datatables.

    Here one more it is a combobox but those uses the same parent and therefore the code is equal.

    http://www.vb-tips.com/ComboboxRelatedDataViewDistinct.ASPX

    Will you be so kind next time to use a description instead of the sentence Help with Visual Basic code. That wants everybody in this forum asking a question.


    Success
    Cor


    Wednesday, October 2, 2013 4:49 PM
  • So for one of my assignments I need to make two listboxes, one with a list of cities and one where depending what city is selected will show the zipcode for that city. There is no display button, it should automatically show the zipcode(s). 

    I have the first part done: 

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load 
    lstCities.Items.Add("Park Ridge") 
    lstCities.Items.Add("Barrington") 
    lstCities.Items.Add("Glen Ellyn") 
    lstCities.Items.Add("Algonquin") 
    lstCities.Items.Add("Crystal Lake") 
    lstCities.SelectedIndex = 0 
    End Sub 

    But when I try to do the second part, the zipcode listbox is always empty. Help?!?! 

    Private Sub lstCities_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstCities.SelectedValueChanged 
    Dim strCity As String 
    strCity = lstCities.SelectedValue 
    Select Case strCity 
    Case "Park Ridge" 
    ListBox1.Text = "60068" 
    Case "Barrington" 
    ListBox1.Text = "60010" & "60011" 
    Case "Glen Ellyn" 
    ListBox1.Text = "60137" & "60138" 
    Case "Algonquin" 
    ListBox1.Text = "60102" 
    Case "Crystal Lake" 
    ListBox1.Text = "60012" 
    End Select 
    End Sub 
    End Class

    Here is what my exact assignment says:

     Code the application. The form’s Load event procedure should add the city names
    shown in Figure 6-63 to the list box and then select the first name in the list. The list
    box’s SelectedValueChanged event procedure should assign the item selected in the
    list box to a variable. It then should use the Select Case statement to display the
    city’s ZIP code(s).

    Do like you are doing except change from lstCities.SelectedValue to lstCities.SelectedIndex and do numbers instead of text. You probably figured out by now that .text did not work as well you thought it would. Anymore and I give it away. ;) good luck. 


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.co.nr

    Thursday, October 3, 2013 12:58 AM