Whenever I try to find an index in a listbox, i get the answer as -1.
here's my code.
Can someone help me why this happens?Code:listbox1.Items.Add("43")
Messagebox.Show(listbox1.Items.IndexOf("43")
Printable View
Whenever I try to find an index in a listbox, i get the answer as -1.
here's my code.
Can someone help me why this happens?Code:listbox1.Items.Add("43")
Messagebox.Show(listbox1.Items.IndexOf("43")
Other than the fact that you are missing the last ) I do not see a problem.
If I add that code to a project then add the ) I get the position in the list where the 43 occurs. i.e. 0 if the list was empty, a greater number if the list already has items when the code is executed.
-1 would indicate the item you are looking for is not there.
Of course the code you posted would not return anything as it will generate a build error.
This code will work
Code:ListBox1.Items.Add("43")
MessageBox.Show(ListBox1.Items.IndexOf("43"))
Nope. In the code I have put the last ')' too.....I don't receive any build error. It is only that I recieve -1 with everything.
So I can only assume that the code you are actually using is different than what you posted as that would have generated a build error as is and with the last ) will work correctly.
Post the code you are actually using and we can look for an issue.
Well, it worked with listbox1.FindStringExact("43") ,so no Issues.
Mustard after the meal...
I think if you had done this :
It would have worked, or am I wrong?Code:ListBox1.Items.Add("43")
MessageBox.Show(ListBox1.Items.IndexOf("43").ToString())
This worked fine when I tried it.
I would imagion it would work the same with the tostring() added on as well.Code:ListBox1.Items.Add("43")
MessageBox.Show(ListBox1.Items.IndexOf("43"))
There was some different problem as the code that was posted would not have executed at all due to the missing ) and once that bracket was added would work.
Yep, this :
Also worked perfectly for me. That is why I am thinking that somewhere he has a setting in ( VB itself ) that causes this. Perhaps Option Strict ( not sure which ) or something is on that causes this stupid behaviour on his machine?Code:ListBox1.Items.Add("43")
MessageBox.Show(ListBox1.Items.IndexOf("43"))
I would think it would trigger a message about confilcting types in such a case but he is reporting that it returned -1 indicating that there was no match.