Create an "Open" dialog in VB

ice

New member
So I have this set of data here and I thought I just had to code that one data set into the program so that with the click of a button the info would display. Turns out I need to have an Open dialog and need to actually have the program open that text file and read the data, inputting it into a table. How exactly do I go about creating an open dialog box that will actually read the text file it opens? I'm using Visual Studio.NET 2003.
 
Ok, I got that part figured out. Now I need some troubleshooting help. Here is my code:

Code:
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim sr As IO.StreamReader
        Dim category, prompt, title, fileName As String
        Dim males, females, perMales, perFemales, total As Double
        Dim fmtStr As String = "{0,-10}{1,12:N0}{2,14:N0}{3,12:P2}{4,14:P2}{5,14:P2}"

        prompt = "Enter the name of the file you wish to display"
        title = "Filename"
        fileName = InputBox(prompt, title)
        sr = IO.File.OpenText(fileName)

        category = sr.ReadLine
        males = CDbl(sr.ReadLine)
        females = sr.ReadLine
        perMales = males / 100115
        perFemales = females / 100115
        total = 100115 / 287713


        'category = sr.ReadLine
        'males = sr.ReadLine
        'females = sr.ReadLine
        'perMales = males / 151228
        'perFemales = females / 151228
        'total = 151228 / 287713

        'category = sr.ReadLine
        'males = sr.ReadLine
        'females = sr.ReadLine
        'perMales = males / 36370
        'perFemales = females / 36370
        'total = 36370 / 287713

        sr.Close()

        With lstGender.Items
            
            .Add(String.Format(fmtStr, category, males, females, perMales, perFemales, total))
            .Add(String.Format(fmtStr, category, males, females, perMales, perFemales, total))
            .Add(String.Format(fmtStr, category, males, females, perMales, perFemales, total))
        End With




    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        Close()

    End Sub
End Class

When I enter the file name it gets the data correctly, but it shows the same piece of data three times in a row. I need to to show all three rows of data, not the same one. I'm sure this has to do with how the data is read because I commented the others out and it displayed the other set.

What do I have to change for it to display all three piece of data in the list?
 
I guess it was easy, I got it working myself. Still need someone to check over it though. Do I even need the parts I commented out? I don't think so because it runs fine with the comment tags there.

Code:
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim sr As IO.StreamReader
        Dim category, prompt, title, fileName As String
        Dim males, females, perMales, perFemales, total As Double
        Dim fmtStr As String = "{0,-10}{1,12:N0}{2,14:N0}{3,12:P2}{4,14:P2}{5,14:P2}"

        prompt = "Enter the name of the file you wish to display"
        title = "Filename"
        fileName = InputBox(prompt, title)
       [color=red] 'sr = IO.File.OpenText(fileName)

        'category = sr.ReadLine
        'males = CDbl(sr.ReadLine)
        'females = sr.ReadLine
        'perMales = males / 100115
        'perFemales = females / 100115
        'total = 100115 / 287713


        'category = sr.ReadLine
        'males = sr.ReadLine
        'females = sr.ReadLine
        'perMales = males / 151228
        'perFemales = females / 151228
        'total = 151228 / 287713

        'category = sr.ReadLine
        'males = sr.ReadLine
        'females = sr.ReadLine
        'perMales = males / 36370
        'perFemales = females / 36370
        'total = 36370 / 287713

        'sr.Close()[/color]

        With lstGender.Items
            sr = IO.File.OpenText("uspopdata.txt")
            category = sr.ReadLine
            males = sr.ReadLine
            females = sr.ReadLine
            .Add(String.Format(fmtStr, category, males, females, males / 100115, females / 100115, 100115 / 287713))

            category = sr.ReadLine
            males = sr.ReadLine
            females = sr.ReadLine
            .Add(String.Format(fmtStr, category, males, females, males / 151228, females / 151228, 151228 / 287713))

            category = sr.ReadLine
            males = sr.ReadLine
            females = sr.ReadLine
            .Add(String.Format(fmtStr, category, males, females, males / 36370, females / 36370, 36370 / 287713))
            sr.Close()

        End With
 
Back
Top