trying to code something in vb.net but have got stuck. when looping through a list of items to see if a source contains them, it only seems to work if the item is first in the list but there is no reason why it should do that. either pm or post here if you think you can help, it would be very much appreciated. Don't particularly want to post here but I have written a very simple test example which will hopefully show the problem I'm having exactly.
I've written a short test class to illustrate my problem Having a lot of problems with a for loop I have written. with the following code, the it only finds the item if it is top of the list. ie in the following example, it finds jim but not jack, james or peter. Any help would be very much appreciated and I'm sure its something simple note, on the form there are two listboxes (lstbox_items, lstbox_log), two buttons (btn_test, btn_loaditems) and one openfiledialog the code is as follows and there is also a text file to load which is simply Code (Text): jim jack james peter Code (Text): Imports System.Net Imports System.Text.RegularExpressions Imports System.IO Imports System.Text Imports System.IO.Compression Imports System.Threading Imports System.Windows.Forms Public Class Form1 Private testthread As Thread Dim streamer As IO.StreamReader Dim TestSource As String Dim CurrentTime As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Control.CheckForIllegalCrossThreadCalls = False End Sub Private Sub btn_loaditems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_loaditems.Click OpenFileDialog1.ShowDialog() Dim itemlistlocation = OpenFileDialog1.FileName streamer = IO.File.OpenText(itemlistlocation) Dim mystring() As String = streamer.ReadToEnd.Split(vbNewLine) lstbox_items.Items.AddRange(mystring) End Sub Private Sub btn_test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_test.Click testthread = New Threading.Thread(AddressOf test) testthread.IsBackground = True testthread.Start() End Sub Private Sub test() Dim items As String = "jim, jack, james, peter" Dim count = lstbox_items.Items.Count - 1 For i = 0 To count Dim currentitem = GetListboxItem(lstbox_items, i) If items.Contains(currentitem) Then lstbox_log.Items.Add("found " & currentitem) Else lstbox_log.Items.Add("not found " & currentitem) End If Next End Sub Private Delegate Function GetListboxItemInvoker(ByVal MyControl As System.Windows.Forms.ListBox, ByVal index As Integer) As String Private Function GetListboxItem(ByVal MyControl As System.Windows.Forms.ListBox, ByVal index As Integer) As String Dim text As String If MyControl.InvokeRequired Then Dim args As Object() = {MyControl, index} text = (MyControl.Invoke(New GetListboxItemInvoker(AddressOf GetListboxItem), args)) Else text = MyControl.Items.Item(index) End If Return text End Function end class
check that make sure that count is coming back as 3. You also might need to declare i. Code (Text): For i As Integer = 0 To count best of help i can offer, I haven't worked with vb.net in 2-3 years, lol.
thanks for the reply heya pretty sure that's not necessarily the problem (and think I might have i declared somewhere else but I missed it when copying and pasting) seeing as the output is Code (Text): found jim not found jack not found james not found peter so it seems to be working through the listbox as I would like, it just doesn't seem to want to find it unless its first in the listbox
think I've fixed it needed to add a .trim in as whitespace had managed to get into the listbox. don't know how that happened but fixed the problem