Click here to Skip to main content
15,915,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friend,

If I use following code then I get error (Additional information: InvalidArgument=Value of '2' is not valid for 'index'.)
VB
Dim index1 As Integer = 0
Dim email As String = ComboBox1.Items.Item(index1).ToString
index1 = index1 + 1


But If I manually put index like 0 or something else then it works. Like following code works


VB
Dim email As String = ComboBox1.Items.Item(0).ToString


Please tell me what's the problem is?
Posted
Updated 25-Jun-14 1:44am
v2
Comments
Thomas Daniels 25-Jun-14 7:45am    
Do you use your code in a loop? If so, can you provide your entire loop?
zubair30 25-Jun-14 7:45am    
I am using a timer

string s = ((yourItemType)yourComboBox.SelectedItem).yourWishProperty; in C#

In this case

Dim email As String = ((string)ComboBox1.SelectedItem).text
 
Share this answer
 
Use this
VB
Dim index1 As Integer = 0 

outside of your function and make your variable public and then use it
 
Share this answer
 
If you use a timer, then index1 becomes probably higher than the highest index of Items. Before you try to get the item from the combobox, check whether it is possible:
VB.NET
If ComboBox1.Items.Count > index1 Then
    Dim email As String = ComboBox1.Items.Item(index1).ToString
    index1 = index1 + 1
Else
    ' index1 is higher than the highest index of the combobox items
End If
 
Share this answer
 
v3
At a guess, that isn't your code: I suspect that index1 is declared outside the method, which means that the value is preserved between executions.

So it you are using a timer as you say, the first Tick event will find index1 contains zero, the second fill find one, and so on.

Why would you increment the index in a timer without checking if the value is still inside the bounds of the combobox items?
 
Share this answer
 
Thanks Guys

My increment was the problem and got it fixed.

Thanks to all friends.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900