locked
Why selecting any combo box item displays last line from text file RRS feed

  • Question

  • Hi,
    Its a Visual Basic Express Edition 2008 Problem.
    I have 1 ComboBox, 1 Label and 1 DatetimePicker on my form and a text file with csv extension which contains names of countries and numeric values separated by commas like first line being : New Zealand, 12 second line New York, -4 and so on and last line Australia, 10.   

    Items in Combobox are inserted from this file.  The code is as under:
     Public Class Form1
        Structure PlacenOffset
            Dim Place As String
            Dim Offset As Integer
            Public Overrides Function ToString() As String
                Return Place
            End Function
        End Structure
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim FILE_NAME As String = "C:\PlaceFile.csv"
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            Do While objReader.Peek() <> -1
                Dim T As PlacenOffset
                T = New PlacenOffset
                Dim TestArray() As String = Split(objReader.ReadLine, ",")
                T.Place = TestArray(0)
                T.Offset = TestArray(1)
                ComboBox1.Items.Add(T)
            Loop
            objReader.Close()
        End Sub
        Public Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
            For Each Item As PlacenOffset In ComboBox1.Items
                Label1.Text = Item.Place & Item.Offset & " time" & vbCrLf & DateTimePicker1.Value.ToLongTimeString & vbCrLf & DateTimePicker1.Value.ToLongDateString
            Next
        End Sub
    End Class
    When I select any item of the combo box it displays in Label1 the name and corresponding numeric value of last line from the text file, I want that if user select New Zealand in the combobox it should display New Zealand from the text file and its corresponding numeric value in the same line and not the particulars of last line from the text file.
    I don't know why it is every time taking last line from the text file when user select any item in the combobox.
    Would appreciate some advice on this.
    Thanks
    RITC29
    Monday, May 18, 2009 11:53 AM

Answers