-
May 15th, 2021, 01:55 PM
#1
Thread Starter
Hyperactive Member
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
-
May 15th, 2021, 03:17 PM
#2
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.
-
May 15th, 2021, 03:33 PM
#3
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)
-
May 15th, 2021, 09:02 PM
#4
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:
If ListBox1.FindStringExact("ALARM") = ListBox.NoMatches Then
'No match was found.
Else
'A match was found.
End If
If you only care if a match was found:
vb.net Code:
If ListBox1.FindStringExact("ALARM") <> ListBox.NoMatches Then
'A match was found.
End If
If you only care if a match was not found:
vb.net Code:
If ListBox1.FindStringExact("ALARM") = ListBox.NoMatches Then
'No match was found.
End If
If you actually want to use the index of the match:
vb.net Code:
Dim matchIndex = ListBox1.FindStringExact("ALARM")
If matchIndex <> ListBox.NoMatches Then
'Use matchIndex here.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|