-
January 8th, 2012, 02:25 AM
#1
[RESOLVED] listbox Help
Whenever I try to find an index in a listbox, i get the answer as -1.
here's my code.
Code:
listbox1.Items.Add("43")
Messagebox.Show(listbox1.Items.IndexOf("43")
Can someone help me why this happens?
-
January 8th, 2012, 03:07 AM
#2
Re: listbox Help
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"))
Always use [code][/code] tags when posting code.
-
January 8th, 2012, 05:29 AM
#3
Re: listbox Help
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.
-
January 8th, 2012, 09:40 AM
#4
Re: listbox Help
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.
Always use [code][/code] tags when posting code.
-
January 8th, 2012, 11:03 AM
#5
Re: listbox Help
Well, it worked with listbox1.FindStringExact("43") ,so no Issues.
-
January 10th, 2012, 11:24 AM
#6
Re: [RESOLVED] listbox Help
Mustard after the meal...
I think if you had done this :
Code:
ListBox1.Items.Add("43")
MessageBox.Show(ListBox1.Items.IndexOf("43").ToString())
It would have worked, or am I wrong?
-
January 10th, 2012, 11:28 AM
#7
Re: [RESOLVED] listbox Help
This worked fine when I tried it.
Code:
ListBox1.Items.Add("43")
MessageBox.Show(ListBox1.Items.IndexOf("43"))
I would imagion it would work the same with the tostring() added on as well.
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.
Always use [code][/code] tags when posting code.
-
January 10th, 2012, 11:32 AM
#8
Re: [RESOLVED] listbox Help
Yep, this :
Code:
ListBox1.Items.Add("43")
MessageBox.Show(ListBox1.Items.IndexOf("43"))
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?
-
January 10th, 2012, 11:40 AM
#9
Re: [RESOLVED] listbox Help
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.
Always use [code][/code] tags when posting code.
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
|