5 Replies - 3422 Views - Last Post: 10 December 2012 - 10:53 PM Rate Topic: -----

#1 andythedog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 12

Reading a Text File, and assigning the arrays to a list box.

Posted 07 December 2012 - 10:32 PM

Hi I am new to Visual Basic so go easy on me please! I am writing a program that reads from a Text File to 6 parallel arrays. The arrays are FirstName(), LastName(),Grade1(),Grade2(),Grade3() and Grade4(). I know that the size of this array should be 5, but I am having trouble writing the code for the arrays. The user clicks the Menu File open menu, which then opens the text file and loads the data into the arrays, then loads the information into a list box.

This is my code I have so far, and whenever I click on the File Open menu, it just shows my error message. Any help would be fantastic, Thank you!
Imports System.IO

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim txtFiles As StreamReader
        txtFiles = File.OpenText("'the place where I store the text file
    End Sub

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click        

'Arrays

        Dim FirstNames() As String = {"FirstName1", "FirstName2", "FirstName3", "FirstName4", "FirstName5","FirstName6"}
        Dim LastNames() As String = {"LastName1", "LastName2", "LastName3", "LastName4", "LastName5", "LastName6"}
        Dim Grade1() As Integer = {100, 100, 80, 100, 90, 0}
        Dim Grade2() As Integer = {30, 50, 20, 0, 10, 40}
        Dim Grade3() As Integer = {90, 30, 20, 100, 90, 0}
        Dim Grade4() As Integer = {0, 10, 0, 30, 40, 100}

        Dim txtFile As IO.StreamWriter
        'text file
        txtFile = File.AppendText("NameData.txt")
        Try
            'writing the information that the user enters to that file
            txtFile.WriteLine(String.Format(FirstNames))
            txtFile.WriteLine(LastNames)
            txtFile.WriteLine(Convert.ToInt32(Grade1))
            txtFile.WriteLine(Convert.ToInt32(Grade2))
            txtFile.WriteLine(Convert.ToInt32(Grade3))
            txtFile.WriteLine(Convert.ToInt32(Grade4))
            txtFile.Close()

            'Message displaying that record is saved
            MessageBox.Show("Record Saved")
        Catch ex As Exception
            MessageBox.Show("Please enter valid information", "Error!")
        End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Reading a Text File, and assigning the arrays to a list box.

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Reading a Text File, and assigning the arrays to a list box.

Posted 08 December 2012 - 08:32 AM

Well here it looks to me as though you're just writing values into a file. Be careful about writing an integer as a string, etc. Good practice to convert everything before you mess with it. :)/>

Why don't you just place the first name, last name, grades, and such all in one line for each person. It'll save you a lot of time. I also use lists this way the data is stored somewhere and then you can edit them and such if that's what you planned on doing.

This way all you need to do is run through each line in the file, add it to the list, and then run through the string arrays of the list.

'Format
'First name, lastname, grade1, grade2, grade3, grade4, grade5
'all delimited by a comma

Dim InfoList as new List(Of String())

Private Sub LoadLists()

Dim Ti as New System.io.Streamreader(application.startuppath & "Tests.txt")

While Ti.peek <> -1

InfoList.add(Ti.Readline.Split(","))

End While

Ti.Close
Ti.Dispose

For each InfoString as String() in InfoList

Listview1.items.add(InfoString(0)) 'first
Listview2.items.add(InfoString(1)) 'last
Listview3.items.add(InfoString(2)) 'grade1
Listview4.items.add(InfoString(3)) 'grade2
Listview5.items.add(InfoString(4)) 'grade3
Listview6.items.add(InfoString(5)) 'grade4
Listview7.items.add(InfoString(6)) 'grade5

Next

End Sub



'I did it this way so that should you want to edit the values of a particular selection in the list you can do so very easily.

This post has been edited by trevster344: 08 December 2012 - 04:14 PM

Was This Post Helpful? 1
  • +
  • -

#3 andythedog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 12

Re: Reading a Text File, and assigning the arrays to a list box.

Posted 08 December 2012 - 01:20 PM

Thank you so much, you have no idea how long I have been working on this! I have it displaying in the list box, but right now it displays the information vertically, is there any way I can get it to write horizontally? The WriteLine property perhaps?

I thought this is what the code below was for, but it does not seem to be working?
While Ti.peek <> -1

InfoList.add(Ti.Readline.Split(","))

End While



Thank you again for explaining things and not just posting the code :)
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Reading a Text File, and assigning the arrays to a list box.

Posted 08 December 2012 - 02:09 PM

View Postandythedog, on 08 December 2012 - 02:20 PM, said:

Thank you so much, you have no idea how long I have been working on this! I have it displaying in the list box, but right now it displays the information vertically, is there any way I can get it to write horizontally? The WriteLine property perhaps?

I thought this is what the code below was for, but it does not seem to be working?
While Ti.peek <> -1

InfoList.add(Ti.Readline.Split(","))

End While



Thank you again for explaining things and not just posting the code :)/>


If you want it that way use a list view control and set its view mode to details I believe. After that the only way you can set I tip that way is to declare a new list view item and pass it a string() array in the constructor. Ill elaborate after I get off of work.

You're welcome.
Was This Post Helpful? 1
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Reading a Text File, and assigning the arrays to a list box.

Posted 08 December 2012 - 04:24 PM

Alright place a Listview control on your form. Set the View property to Details. Now you can add static columns or you can use code to add columns via the Listview1.Columns.Add method. Now the best way to add an array of strings into a list that will align them in the proper columns is to first ensure the proper values will be going into the proper dynamic or static columns you've created, and then declare a new list view item.

Dim String1(1) as String()
String1(0) = {"Trevor", "Coder", "A", "B", "C", "D", "F"}

Dim NewListView as New ListViewItem(String1(0))

ListView1.Items.Add(NewListView)



This will place the following values in the available columns. If there are less columns than values certain values will of course not be added.

------------Columns-----------------

Trevor | Coder | A | B | C | D | F | 'The list view item added, and how it should display

This post has been edited by trevster344: 08 December 2012 - 04:25 PM

Was This Post Helpful? 1
  • +
  • -

#6 andythedog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-November 12

Re: Reading a Text File, and assigning the arrays to a list box.

Posted 10 December 2012 - 10:53 PM

Wow Thank you that helps a lot! I am just having problems with another part of my code now :(
Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1