I need to make a program where you can put in last name and major and it saves it to a file. I have the combo box working now but when I fill in everything and
press the button i get this:
An unhandled exception of type 'System.IndexOutOfRangeException'
occurred in CS116_Project2_Nix.exe
Additional information: Index was outside the bounds of the array.
--- and it highlights this
If major(i) = combobox.Text Then
combobox.Items.Add(i)
End If
here is my full code:
Dim major() As String
Dim upperbound As Integer
Dim i As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'1. create a string variable to hold the filename
Dim textfile As String
'2. access the openfiledialog control and assign the selected
filename to your variable.
OpenFileDialog1.ShowDialog()
textfile = OpenFileDialog1.FileName
'3. declare a streamreader variable and open the file name
Dim sr As IO.StreamReader = IO.File.OpenText(textfile)
Do While sr.Peek <> -1
combobox.Items.Add(sr.ReadLine)
upperbound = upperbound + 1
Loop
sr.Close()
'4. get the records from the input file and load them into an
array (p. 309)
ReDim major(upperbound)
sr = IO.File.OpenText(textfile)
For i = 1 To upperbound
major(i) = sr.ReadLine
Next
'6. load the contents of the array into the combo box. a for
loop would be a good way to do this (p.306)
End Sub
Private Sub btnMajor_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnMajor.Click
If major(i) = combobox.Text Then
combobox.Items.Add(i)
End If
'5. clear combo box
combobox.Items.Clear()
'1. make sure the text property is not empty, if empty display
box and exit procedure
If (txtName.Text <> "") And (combobox.Text <> "") Then
MsgBox("Incomplete Information.")
'2. declare string variables for the last name, selected
major and the name of the output file
Dim lastname As String
Dim selectedmajor As String
Dim outputfile As String
'3. access the savefiledialog control and assign the
selected filename to the variable that you created for the filename,
SaveFileDialog1.ShowDialog()
outputfile = SaveFileDialog1.FileName
'4. declare a streamwriter variable and create the output
file using the createtext method of io.file )p.406)
Dim sw As IO.StreamWriter = IO.File.CreateText("inputlist.text")
sw.WriteLine("inputlist.txt")
sw.Close()
'5. write the last name and major to the output file
sw.WriteLine(txtName.Text)
sw.WriteLine(combobox.Text)
'6. close the output file
sw.Close()
txtName.Clear()
Else : MsgBox("Your must enter last name and major.")
End If
'7. display a message to the user to indicate that the
operation is complete.
MsgBox("Information Complete")
End Sub
End Class
press the button i get this:
An unhandled exception of type 'System.IndexOutOfRangeException'
occurred in CS116_Project2_Nix.exe
Additional information: Index was outside the bounds of the array.
--- and it highlights this
If major(i) = combobox.Text Then
combobox.Items.Add(i)
End If
here is my full code:
Dim major() As String
Dim upperbound As Integer
Dim i As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'1. create a string variable to hold the filename
Dim textfile As String
'2. access the openfiledialog control and assign the selected
filename to your variable.
OpenFileDialog1.ShowDialog()
textfile = OpenFileDialog1.FileName
'3. declare a streamreader variable and open the file name
Dim sr As IO.StreamReader = IO.File.OpenText(textfile)
Do While sr.Peek <> -1
combobox.Items.Add(sr.ReadLine)
upperbound = upperbound + 1
Loop
sr.Close()
'4. get the records from the input file and load them into an
array (p. 309)
ReDim major(upperbound)
sr = IO.File.OpenText(textfile)
For i = 1 To upperbound
major(i) = sr.ReadLine
Next
'6. load the contents of the array into the combo box. a for
loop would be a good way to do this (p.306)
End Sub
Private Sub btnMajor_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnMajor.Click
If major(i) = combobox.Text Then
combobox.Items.Add(i)
End If
'5. clear combo box
combobox.Items.Clear()
'1. make sure the text property is not empty, if empty display
box and exit procedure
If (txtName.Text <> "") And (combobox.Text <> "") Then
MsgBox("Incomplete Information.")
'2. declare string variables for the last name, selected
major and the name of the output file
Dim lastname As String
Dim selectedmajor As String
Dim outputfile As String
'3. access the savefiledialog control and assign the
selected filename to the variable that you created for the filename,
SaveFileDialog1.ShowDialog()
outputfile = SaveFileDialog1.FileName
'4. declare a streamwriter variable and create the output
file using the createtext method of io.file )p.406)
Dim sw As IO.StreamWriter = IO.File.CreateText("inputlist.text")
sw.WriteLine("inputlist.txt")
sw.Close()
'5. write the last name and major to the output file
sw.WriteLine(txtName.Text)
sw.WriteLine(combobox.Text)
'6. close the output file
sw.Close()
txtName.Clear()
Else : MsgBox("Your must enter last name and major.")
End If
'7. display a message to the user to indicate that the
operation is complete.
MsgBox("Information Complete")
End Sub
End Class