hi

i have to open a text file from an OpenFileDialog as an Array into a listbox. The data in the text file are dates and a number in this format, "01/06/2010,106". what i need to do then is get the the 5 highest dates and the 5 lowest dates and display them in label, ie lblHigh and lblLow. Please help

Recommended Answers

All 6 Replies

You have a CSV file, or a Comma Seperated Value file. To get the highest and lowest dates form the file, after opening and reading it into an arra, of course, is to simply use sort:

' Sort the array
Array.Sort()

Dim HighestValue = Array(0)               ' First value is the highest.
Dim LowestValue = Array(Array.Length-1)   ' Last value is the lowest.
Imports System.IO

Public Class Form1
    Dim dts(10) As Date
    Dim Qtys(10) As Integer

    Private Sub OpenToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem1.Click

        Dim i As Integer = 0
        Dim sr As StreamReader = Nothing
        openFD.Filter = "Text Files (*.txt)|*.txt"
        If openFD.ShowDialog() = DialogResult.OK Then
            Try
                sr = New StreamReader(My.Computer.FileSystem.ReadAllText(openFD.FileName))
                lblCourse.Text = sr.ReadLine 'get the course outside the loop

                While sr.Peek > 0
                    Dim arr() As String = sr.ReadLine.Split(","c)
                    dts(i) = CDate(arr(0))
                    Qtys(i) = CInt(arr(1))

                    'resize arrays in blocks of 10
                    If i Mod 10 = 0 Then
                        ReDim Preserve dts(i + 10)
                        ReDim Preserve Qtys(i + 10)
                    End If
                    i += 1

                End While
                'When all read resize arrays to final size
                ReDim Preserve dts(i - 1)
                ReDim Preserve Qtys(i - 1)
            Catch ex As Exception
                MsgBox("An Error Occured." & vbCrLf & ex.Message)

            End Try
        End If
    End Sub

    Private Sub LoadListbox()
        Dim i As Integer
        For i = 0 To dts.Length - 1
            ListBox1.Items.Add(dts(i).ToShortDateString + " - " + Qtys(i).ToString)
        Next

        'sort by values
        Array.Sort(Qtys, dts)

        'display high and low
        lblLow.Text = dts(0).ToShortDateString + " - " + Qtys(0).ToString
        lblHigh.Text = dts(dts.Length - 1).ToShortDateString + " - " + Qtys(Qtys.Length - 1).ToString
    End Sub
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub


End Class

That is the code i have, where am i going wrong?

Array is the reference to the actual array.

' sort the arrays
dts.Sort()
Qtys.Sort()

' Results
Dim LowestDate As Date = dts(0)
Dim HighestDate as Integer = dts(dts.Length - 1)

Dim LowestQuantity as Integer = Qtys(0)
Dim HighestQuantity as Integer = Qts(Qts.Length - 1)

' You can build the array another way as well
Dim HighestDates() As Date = {dts(0), dts(1), dts(2), dts(3), dts(4)}
Dim HighestQuantities() As Date = {Qtys(0), Qtys(1), Qtys(2), Qtys(3), Qtys(4)}

To everyone who has posted code so far: We generally do not post solutions unless the OP has gone to some effort to solve the problem on his/her own. In this case, the OP has posted what is quite likely a homework assignment and provided no proof that any attempt was made to solve the problem. If we post the solution then the OP has learned nothing from the assignment. We have effectively robbed the OP of part of his/her education.

Furthermore, if the marker does a google search that leads to this site then the OP will be labeled as a plagerizer. At some educational institutions this can lead to expulsion.

i didn't realise this, i truely am sorry. if you know of a place where i can find the information i need to learn how to do this then please tell me and i'll be more than happy to go there and learn.
Thank you

Is this an assignment for a programming course?

Just about any beginning VB book will tell you how to program the OpenFileDialog, how to do simple text input, and how to use the ListBox control. What books do you have available, and have you actually read them and tried to use any of the components that you'll need? If you don't have a book then try the Intro VB Series available online here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.