473,421 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Error Code and how to get it to disappear

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
3 1159
kadghar
1,295 Expert 1GB
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
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
1,295 Expert 1GB
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

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

Similar topics

11
by: varois83 | last post by:
Hi I am in the process of creating a guestbook for my site, I am a newbie and used several tutorials and customized them to what I need using the little knowledge I got. I get the following...
1
by: Bil Click | last post by:
I am coding classic ASP pages in Visual Studio .Net 2003. I have a file called counties_option.asp that just has a list of options: <OPTION VALUE="030">ANSON</OPTION> <OPTION...
4
by: jmensch | last post by:
I'm reasonably new to JavaScript and DHTML, so I'm sorry if this is an obvious mistake. The following snippet of code is intended to make the menu -- the absolute-positioned <div> section --...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
10
by: robert d via AccessMonster.com | last post by:
I have a global error handler that up until today has been working flawlessly. Let me first provide the relevant code **************************************************************** On Error...
3
by: hyd | last post by:
Hello, I use VS2005 - C++/CLI. I have some kind of events in native C++. I want to raise .NET events when my native C++ events occur. So, I wrote some code for that but I have an C1001 error (An...
2
by: nickyeng | last post by:
I have a class, and i try to create an object of it but it give me error in compilation. The error is something like "]b]undefined reference to 'Game::PowerUp::powerups", in constructor of...
15
by: Dave | last post by:
I am getting the error above intermittantly with an ASP 3.0 page using an MS Access 2003 database. I have searched Google extensively and found the following possible causes for this error: A...
3
by: rorni | last post by:
Hi, I'm porting code from Windows to HP-UX 11, compiling with g++. I'm getting a compilation error on the system's debug.h include file, which is included very indirectly through a series of...
9
by: ahilar12 | last post by:
1. <head> 2. <script type="text/javascript"> 3. </script> 4. </head> 5. <body> 6. <form> 7. <select name="team" id="mylist" > 8. <option></option> 9....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.