Help! IndexOutOfRangeException'

renix

New member
Joined
Apr 24, 2006
Messages
3
Programming Experience
Beginner
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
 
in btnMajor_Click you start by testing If major(i) = combobox.Text. What value is 'i' here? is it the same value each time you click that button? is 'i' supposed to be a specific value here? are you perhaps missing a loop since you conditionally use combobox.Items.Add(i) afterwards? could this be homework? ("find the missing code..")
 
its a project that we have to write the whole code for. i've put this all together but it still doesn't work and i'm stuck
 
This is the assignment:

For this project, you will create a Visual Basic application that gets data from a text file, whose location is provided by an OpenFileDialog control, loads it into an array, then uses the array to fill a DropDown combo box control. The text file contains a list of majors. When the user chooses a major from the list, the user’s last name and selected program will be written to a text file whose name and location will be provided by a SaveFileDialog control.

Be sure that you read the instructions carefully, and that all of the required elements are present in your application. Test your application! Test it several times, with a variety of inputs! This project is worth a significant part of your final grade, as much as all of the labs combined; it is worth your while to take it seriously.

First, create your interface. You will need:
• A text box for the user to enter his or her last name, and a label to tell the user what to enter in the text box. The text property of the text box should be null or empty.
• A combo box to hold the list of majors. The dropdownstyle property of this combo box should be DropDownList. You will need a label next to the combo box to tell the user what to do (e.g. “Select a Major”)
• A button that reads “Save Major to File”
• An OpenFileDialog control. Set the filter property to: Text files (*.txt)| *.txt
• A SaveFileDialog control. Set the filter property the same as the OpenFileDialog control.

Now, add code to your form
Choosing the location of the input text file, loading it into an array, and filling the combo box are all done before the user sees the form, in the Form Load event. To access the Form.Load event, double-click on the background grid of your interface.
1. Create a string variable to hold the filename.
2. Access the OpenFileDialog control (see p. 461 of your textbook for examples) and assign the selected filename to your variable
3. Declare a streamreader variable and open the file name (see top of p. 462)
4. Get the records from the input file and load them into an array (see p. 309 of your text for an example) Note: be sure to close and re-open the file in between getting the number of records and reading the records into the array
5. Clear the combo box
6. Load the contents of the array into the combo box. A For loop would be a good way to do this (see p. 306 for an example that uses a list box, which is very similar to a combo box).

Once the user has entered a last name and selected a major, clicking the button will save the data on the form to a text file. This will be done in the button’s Click event.
1. Make sure that the text property of the text box and the text property of the combo box are not empty. If the user has not entered data for both, display a message and exit the event procedure.
2. Declare string variables for the last name, selected major and the name of the output file (3 string variables)
3. Access the SaveFileDialog control and assign the selected filename to the variable that you created for the file name. This works a lot like the OpenFileDialog statement, except that you use the keyword SaveFileDialog1 instead of OpenFileDialog1.
4. Declare a streamwriter variable and create the output file using the CreateText method of IO.File (see p. 406 of your text for examples).
5. Write the last name and the major to the output file
6. Close the output file
7. Display a message to the user to indicate that the operation is complete.
 
Back
Top