Results 1 to 4 of 4

Thread: ListBox items will add infinitely

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    ListBox items will add infinitely

    Hey, You might miss my previous tread.
    It is not duplicated.

    Here's the thing:
    There's a list box which several items need to be added in it when their own conditions would be true, in a 1 second timer.

    Code:
    Private sub Timer1_Tick()
    If AlarmCondition = True Then
    
    If ListBox1.FindStringExact ("ALARM") Then
    ListBox1.Items.Add ("ALARM")
    'Do alarm condition thing
    End If
    
    end If
    End sub
    Aforementioned code will properly do when list box items are clear.
    But when items quantity to check increases (and few of them are in ALARM condition,) Similar alarms will add to the listbox infinitely. Like it trapped in a loop. (If true condition -> Timer 1s -> Add item -> Repeat).

    Is there any other property/method/way like .FindStringExact which I am not aware of? Frankly it acts like a super bug. plz help

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: ListBox items will add infinitely

    https://docs.microsoft.com/en-us/dot...t?view=net-5.0

    Based on the possible return values as documented, I suspect that your code treating the return value as a Boolean is the reason.

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: ListBox items will add infinitely

    At each tick of the timer, each items with the alarm condition being true will be added each time, again and again... you need to add a new condition to each item : already_included = true, so it doesn't add it to the list box after the first one or you need to set the alarm condition to false at the end of the tick event.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: ListBox items will add infinitely

    You should turn Option Strict On immediately. It would have refused to compile that bad code and you would have then been forced to find out why it was bad and fix it. If you had read the documentation as you should have, or even just paid attention to Intellisense as you wrote the code, then you would have seen that FindStringExact doesn't return a Boolean but rather an Integer and it is up to you to process that Integer appropriately to be able to test a Boolean expression. As the documentation states, if a match is found then its zero-based index is returned and if no match is found then ListBox.NoMatches, which has a value of -1, is returned. By implicitly treating that result as a Boolean, you're getting False when a match is found at index zero and True if a match is found at any other index AND if no match is found at all. That obviously makes no sense at all. If you wrote that code properly and tested a Boolean expression, as Option Strict On would force you to do, it would look something like this:
    vb.net Code:
    1. If ListBox1.FindStringExact("ALARM") = ListBox.NoMatches Then
    2.     'No match was found.
    3. Else
    4.     'A match was found.
    5. End If
    If you only care if a match was found:
    vb.net Code:
    1. If ListBox1.FindStringExact("ALARM") <> ListBox.NoMatches Then
    2.     'A match was found.
    3. End If
    If you only care if a match was not found:
    vb.net Code:
    1. If ListBox1.FindStringExact("ALARM") = ListBox.NoMatches Then
    2.     'No match was found.
    3. End If
    If you actually want to use the index of the match:
    vb.net Code:
    1. Dim matchIndex = ListBox1.FindStringExact("ALARM")
    2.  
    3. If matchIndex <> ListBox.NoMatches Then
    4.     'Use matchIndex here.
    5. End If
    You should immediately turn Option Strict On in the project properties and also in the VS options, so it is On by default for all future projects.

Tags for this Thread

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