CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2011
    Posts
    19

    [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?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  3. #3
    Join Date
    Dec 2011
    Posts
    19

    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.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  5. #5
    Join Date
    Dec 2011
    Posts
    19

    Re: listbox Help

    Well, it worked with listbox1.FindStringExact("43") ,so no Issues.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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?

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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?

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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
  •  





Click Here to Expand Forum to Full Width

Featured