By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,988 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,988 IT Pros & Developers. It's quick & easy.

Error Code and how to get it to disappear

P: 11
Here is the code again and the error I get is Invalid Argument=Value of "-1" is not valid for 'index' Parameter Name:index

Please advise the code so this error goes away and the program will still execute correctly. Other suggestions posted either still give errors or the program does not execute once I put the suggestion in. Thanks for your help.
Jo Ann


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'load the item into the combobox
Try

vinStreamreader = New StreamReader("car.txt")
DisplayRecord()
Catch ex As Exception
'File is not found.
MessageBox.Show(ex.Message)
End Try


End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'Read the next record.

DisplayRecord()
End Sub

Private Sub DisplayRecord()
'Read and display the next record.
Dim vinString As String
Do While vinStreamreader.Peek <> -1
vinString = vinStreamreader.ReadLine()
Me.ComboBox1.Text = vinString
Me.ComboBox1.Items.Add(ComboBox1.Text)


Me.ListBox1.Items.Add(vinStreamreader.ReadLine())
Me.ListBox2.Items.Add(vinStreamreader.ReadLine())
Me.ListBox3.Items.Add(vinStreamreader.ReadLine())

Loop
'Display labels
Me.Label4.Text = Me.ListBox1.Items(Me.ComboBox1.SelectedIndex)
Me.Label5.Text = Me.ListBox2.Items(Me.ComboBox1.SelectedIndex)
Me.Label6.Text = Me.ListBox3.Items(Me.ComboBox1.SelectedIndex)
End Sub


End Class
Nov 26 '07 #1
Share this Question
Share on Google+
3 Replies


kadghar
Expert 100+
P: 1,295
Here is the code again and the error I get is Invalid Argument=Value of "-1" is not valid for 'index' Parameter Name:index

...
When the combobox has no selected item, its index is -1.
I think this problem can be solved by changing Me.Combobox1.SelectedIndex with an integer, lets say K, and :

Expand|Select|Wrap|Line Numbers
  1. dim K as integer
  2. K= me.combobox1.selectedindex
  3. if K<0 then K = 0
This way, if there's no item selected, it'll use the first of the list. Or you can just exit the sub if the index is -1.

HTH
Nov 26 '07 #2

P: 11
I tried this code at the very beginning and at each sub and I still get the message. Good idea but does not eliminate the error. Jo Ann
Nov 26 '07 #3

kadghar
Expert 100+
P: 1,295
I tried this code at the very beginning and at each sub and I still get the message. Good idea but does not eliminate the error. Jo Ann
could you put again your code, but with code tags, so it'll numerate the lines, and tell us in what line does the error first appear??

Thanks =)
Nov 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.