Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

For Each Loop with a ListBox - Only finding last city

$
0
0
So I'm going through my readings for my next upcoming homework and I decided to try out the example given in the book, which I usually just read and understand (if not I go back to it).

Public Class SearchCities

    Private Sub btnFindCity_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindCity.Click

        For Each strCity As String In lstCities.Items
            If strCity = txtCity.Text Then
                lblResult.Text = "The City was found!"
            Else
                lblResult.Text = txtCity.Text & " is not in the list of " & vbNewLine & "available cities"
            End If
        Next

    End Sub
End Class



Now the "Else" Statement is what I added because I wanted to see if it worked like I thought it would, which it doesn't work. The form is simple, a ListBox with 6 Items, Find Cities button, and Label for if the city was found to display the result.

It only seems to be catch the last city in the list box and giving a successful result, every other city claims it isn't in the list and I'm confused as to why this is happening.

I've uploaded my project (VS2010)


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>