ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each?
ListBox Alternative to For Each? ListBox Alternative to For Each?
ListBox Alternative to For Each?
Go Back  Xtreme Visual Basic Talk > > > ListBox Alternative to For Each?


Reply
 
Thread Tools Display Modes
  #1  
Old 02-07-2016, 05:42 PM
CodeTalk CodeTalk is offline
Newcomer
 
Join Date: May 2015
Posts: 8
Default ListBox Alternative to For Each?


Code:
  For Each LItem In ListBox1.Items
            'If String1 found in list then
            If LItem.ToString = String1 Then
                MsgBox(String1Found)
            Else
                'If String1 not found then add
                ListBox1.Items.Add(String1)
                MsgBox(String1Added)
            End If
        Next
When I attempt this logic I receive an error:
"Additional information: List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."

The above code sample would not work correctly even without an error as it would add String1 for any string in the Listbox that doesn't say String1...

How would I correct this For Each loop so that I can detect if an item is in the list and write to the list if item isn't found?

Last edited by CodeTalk; 02-07-2016 at 06:04 PM.
Reply With Quote
  #2  
Old 02-07-2016, 06:15 PM
CodeTalk CodeTalk is offline
Newcomer
 
Join Date: May 2015
Posts: 8
Default

Code:
  If ListBox1.Items.Contains("String1") = True Then
            MsgBox("String1 Exists")
        Else
            ListBox1.Items.Add("String1")
        End If
Found the answer. I figured I would require a loop of some sort but it turns out it's way simpler...

Last edited by CodeTalk; 02-07-2016 at 06:22 PM.
Reply With Quote
Reply

Tags
list, string1, enumerator, found, litem, modified, bound, listbox1.items, loop, write, correct, change, alternative, msgboxstring1added, add, listbox1.items.addstring1, attempt, listbox, additional, information, litem.tostring, error, logic, receive, msgboxstring1found


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each? ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each? ListBox Alternative to For Each?
ListBox Alternative to For Each?
ListBox Alternative to For Each?
 
ListBox Alternative to For Each?
ListBox Alternative to For Each?
 
-->