This program uses an array of structures to hold membership information that is read in from a sequential.
my problem is I can not get anything to load in my listbox, I am getting no errors. Below My code is how I am supposed to design the program.
Step 2 – Design the Form:
Design the form as shown in Figure 1. You will need one button control, one list box, and one picture box. Load the downloaded galaxy.jpg image into the picture box.
Step 3 – Add the text file to the project:
Copy the downloaded text file to the GalaxyTrekkers folder using Windows Explorer. In the VB.Net IDE, click on the Project menu and select Add existing item. Browse to the GalaxyTrekkers folder. In the File Type combo at the bottom of the Add Existing dialog, select All Files. You will now see the downloaded file StudentList.txt. Select it and click on Add. The text file will now appear in the Solution Explorer. By doing this, you are now able to view the contents of the file within the IDE by double-clicking its name in Solution Explorer.
Look at the file contents. You will see that there are 6 fields for each record, separated by commas. The fields are ID, last name, first name, middle initial, grade, and class period. These fields will need to be declared as member variables in a structure. Treat all of these fields as type String.
Step 4 – Declare the form-level variables:
Declare a structure named Members that contains a member variable for each of the fields in the text file.
Declare an array of type Member with a size of 250.
Step 5 – Write a procedure to read the text file and load the list box:
Chapter 9 presented a sample program that read the entire contents of a text file into memory and then parsed out each record by searching for the occurrence of a new line indicator (ControlChars.NewLine). In this program, you will do that, but after reading in one line of the text file, you must also break out the individual fields.
For each line in the text file, create a new structure of type Member, and load the individual member variables of the structure with the values from the text file line. Then add that structure to the next available position in the array of structures.
When the entire file has been read in and loaded into structures in the array, then load the list box by reading the array. Only the ID, last name and first name fields will be loaded into the list box. See Figure 2 for details of how the list box contents should appear.
Hints:
1 – Use a For loop to break out the fields in each line of the text file, because you know how many fields there are.
my problem is I can not get anything to load in my listbox, I am getting no errors. Below My code is how I am supposed to design the program.
VB.NET:
Public Class Form1
'Define the Members Structure
Structure Members
Public Id As String
Public LastName As String
Public FirstName As String
Public MiddleIntial As String
Public Grade As String
Public ClassString As String
End Structure
'Declare Array
Private Member(250) As Members
Dim Students As String
Dim Record As String
Dim newLineIndex As Integer
Dim newIndex As Integer
Dim commaIndex As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
'Fills the Member Array and membersListBox
'With the Data stored in the sequential access File
If My.Computer.FileSystem.FileExists("StudentList.TXT ") Then
' If the file exists, assign its contents to the Students Variable
Students = My.Computer.FileSystem.ReadAllText("studentList.txt")
Else
MessageBox.Show("error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
For subscript As Integer = 1 To 6
' Locate the newline character in the Students variable
newLineIndex = Students.IndexOf(ControlChars.NewLine, newIndex)
' Assigns a line from the Students variable to the Record variable
Record = Students.Substring(newIndex, newLineIndex - newIndex)
'Locate the comma in the Record Variable
commaIndex = Record.IndexOf(",", 0)
'Assigns ID , LastName, FirstName, MiddleInitial, Grade and ClassString to the array
Member(subscript).Id = Record.Substring(0, commaIndex)
Member(subscript).LastName = Record.Substring(commaIndex + 1)
Member(subscript).FirstName = Record.Substring(commaIndex + 1)
Member(subscript).MiddleIntial = Record.Substring(commaIndex + 1)
Member(subscript).Grade = Record.Substring(commaIndex + 1)
Member(subscript).ClassString = Record.Substring(commaIndex + 1)
'Add items to the listBox
membersListBox.Items.Add(Member(subscript).Id)
membersListBox.Items.Add(Member(subscript).LastName)
membersListBox.Items.Add(Member(subscript).FirstName)
'update the newIndex
newIndex = newLineIndex + 5
Next subscript
End If
End Sub
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
End Class
Step 2 – Design the Form:
Design the form as shown in Figure 1. You will need one button control, one list box, and one picture box. Load the downloaded galaxy.jpg image into the picture box.
Step 3 – Add the text file to the project:
Copy the downloaded text file to the GalaxyTrekkers folder using Windows Explorer. In the VB.Net IDE, click on the Project menu and select Add existing item. Browse to the GalaxyTrekkers folder. In the File Type combo at the bottom of the Add Existing dialog, select All Files. You will now see the downloaded file StudentList.txt. Select it and click on Add. The text file will now appear in the Solution Explorer. By doing this, you are now able to view the contents of the file within the IDE by double-clicking its name in Solution Explorer.
Look at the file contents. You will see that there are 6 fields for each record, separated by commas. The fields are ID, last name, first name, middle initial, grade, and class period. These fields will need to be declared as member variables in a structure. Treat all of these fields as type String.
Step 4 – Declare the form-level variables:
Declare a structure named Members that contains a member variable for each of the fields in the text file.
Declare an array of type Member with a size of 250.
Step 5 – Write a procedure to read the text file and load the list box:
Chapter 9 presented a sample program that read the entire contents of a text file into memory and then parsed out each record by searching for the occurrence of a new line indicator (ControlChars.NewLine). In this program, you will do that, but after reading in one line of the text file, you must also break out the individual fields.
For each line in the text file, create a new structure of type Member, and load the individual member variables of the structure with the values from the text file line. Then add that structure to the next available position in the array of structures.
When the entire file has been read in and loaded into structures in the array, then load the list box by reading the array. Only the ID, last name and first name fields will be loaded into the list box. See Figure 2 for details of how the list box contents should appear.
Hints:
1 – Use a For loop to break out the fields in each line of the text file, because you know how many fields there are.