-
Nov 30th, 2015, 06:49 AM
#1
Thread Starter
New Member
InvalidArgument value of '1' is not valid for 'index' Help!!!
When the user input the customer name and then click the button “find”, your program can search the customer history record by the customer name. If the customer name is found in the history record, the message box will be shown "__ is on line _ of the list."
If the customer name cannot be found in the history record, the message will be shown "Customer not found".
Code:
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim i As Integer
For i = 1 To 999
If lstCusHis.Items(i) = txtFind.Text Then
MsgBox(txtFind.Text & " is on line " & i + 1 & " of the list.")
End If
If lstCusHis.Items(i) <> txtFind.Text Then
MsgBox("Customer not found")
End If
Next
End Sub
-
Nov 30th, 2015, 08:10 AM
#2
Re: InvalidArgument value of '1' is not valid for 'index' Help!!!
Your code is wrong in a number of ways. With regards to the issue, this:should be this:
Code:
For i = 0 To lstCusHis.Items.Count - 1
You should always base the limits of a For loop on the limits of the list you're enumerating. The first index in any array or collection is 0, not 1, the the last index is based on the Count or Length.
Another is the fact that you are telling the user that their item is not found for EVERY item in the list that doesn't match. That means that if their entry matches the last itme in the list, you will tell them that there entry was not found once for every other item in the list. You need to get that second message OUT of the loop and ONLY display it if a match is not found anywhere in the list.
Finally, you are continuing to look for a match even after you find one. If there are hundreds of entries and you match the first one, what's the point of testing the rest?
-
Nov 30th, 2015, 10:27 AM
#3
Member
Re: InvalidArgument value of '1' is not valid for 'index' Help!!!
Originally Posted by katrina97
When the user input the customer name and then click the button “find”, your program can search the customer history record by the customer name. If the customer name is found in the history record, the message box will be shown "__ is on line _ of the list."
If the customer name cannot be found in the history record, the message will be shown "Customer not found".
Code:
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim i As Integer
For i = 1 To 999
If lstCusHis.Items(i) = txtFind.Text Then
MsgBox(txtFind.Text & " is on line " & i + 1 & " of the list.")
End If
If lstCusHis.Items(i) <> txtFind.Text Then
MsgBox("Customer not found")
End If
Next
End Sub
Hello katrina,
what is the lstCusHis? Is it a listbox?
If so, you can directly use the following without any For Loops:
Code:
If lstCusHis.Items.Contains(txtFind.Text) Then
MsgBox(String.Format ("{0} is on line {1}", txtFind.Text, lstCusHis.Items.IndexOf(txtFind.Text).ToString()))
End If
-
Nov 30th, 2015, 10:53 AM
#4
Re: InvalidArgument value of '1' is not valid for 'index' Help!!!
Originally Posted by tassoma
Hello katrina,
what is the lstCusHis? Is it a listbox?
If so, you can directly use the following without any For Loops:
Code:
If lstCusHis.Items.Contains(txtFind.Text) Then
MsgBox(String.Format ("{0} is on line {1}", txtFind.Text, lstCusHis.Items.IndexOf(txtFind.Text).ToString()))
End If
Quite so, but it's rather inefficient to call Contains and IndexOf. It would be more efficient to call just IndexOf and then test whether that is -1 or not. Also, there's no need to call ToString on something that's being passed as an argument to String.Format. That is done implicitly.
-
Nov 30th, 2015, 11:10 AM
#5
Member
Re: InvalidArgument value of '1' is not valid for 'index' Help!!!
Yes, you are right jmcilhinney .. IndexOf only will be efficient.
Also no need for ToString() .. it takes Object argument ..
-
Nov 30th, 2015, 11:18 PM
#6
Thread Starter
New Member
Re: InvalidArgument value of '1' is not valid for 'index' Help!!!
Originally Posted by tassoma
Hello katrina,
what is the lstCusHis? Is it a listbox?
If so, you can directly use the following without any For Loops:
Code:
If lstCusHis.Items.Contains(txtFind.Text) Then
MsgBox(String.Format ("{0} is on line {1}", txtFind.Text, lstCusHis.Items.IndexOf(txtFind.Text).ToString()))
End If
lstCusHis is a listbox.
It must be For...Next statement.
It doesn't work.
-
Dec 1st, 2015, 06:46 AM
#7
Re: InvalidArgument value of '1' is not valid for 'index' Help!!!
Originally Posted by katrina97
lstCusHis is a listbox.
It must be For...Next statement.
It doesn't work.
If you have to use a For loop then why even try something that doesn't contain a For loop? I told you how to modify your code to work with a For loop in my first post. If you have to use a For loop then maybe you should try that.
-
Dec 4th, 2015, 03:54 AM
#8
Thread Starter
New Member
Re: InvalidArgument value of '1' is not valid for 'index' Help!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|