[vb.net] Small Problemo

Discussion in 'Code Snippets and Tutorials' started by chelsea1, May 6, 2012.

  1. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    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.
     
  2. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    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):
    1. jim
    2. jack
    3. james
    4. peter
    Code (Text):
    1. Imports System.Net
    2. Imports System.Text.RegularExpressions
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.IO.Compression
    6. Imports System.Threading
    7. Imports System.Windows.Forms
    8.  
    9. Public Class Form1
    10.  
    11.     Private testthread As Thread
    12.     Dim streamer As IO.StreamReader
    13.     Dim TestSource As String
    14.     Dim CurrentTime As String
    15.  
    16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    17.         Control.CheckForIllegalCrossThreadCalls = False
    18.     End Sub
    19.  
    20.  
    21.     Private Sub btn_loaditems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_loaditems.Click
    22.         OpenFileDialog1.ShowDialog()
    23.         Dim itemlistlocation = OpenFileDialog1.FileName
    24.         streamer = IO.File.OpenText(itemlistlocation)
    25.         Dim mystring() As String = streamer.ReadToEnd.Split(vbNewLine)
    26.         lstbox_items.Items.AddRange(mystring)
    27.     End Sub
    28. Private Sub btn_test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_test.Click
    29.         testthread = New Threading.Thread(AddressOf test)
    30.         testthread.IsBackground = True
    31.         testthread.Start()
    32.     End Sub
    33.     Private Sub test()
    34.         Dim items As String = "jim, jack, james, peter"
    35.         Dim count = lstbox_items.Items.Count - 1
    36.         For i = 0 To count
    37.             Dim currentitem = GetListboxItem(lstbox_items, i)
    38.             If items.Contains(currentitem) Then
    39.                 lstbox_log.Items.Add("found " & currentitem)
    40.             Else
    41.                 lstbox_log.Items.Add("not found " & currentitem)
    42.             End If
    43.         Next
    44.     End Sub
    45. Private Delegate Function GetListboxItemInvoker(ByVal MyControl As System.Windows.Forms.ListBox, ByVal index As Integer) As String
    46. Private Function GetListboxItem(ByVal MyControl As System.Windows.Forms.ListBox, ByVal index As Integer) As String
    47.         Dim text As String
    48.         If MyControl.InvokeRequired Then
    49.             Dim args As Object() = {MyControl, index}
    50.             text = (MyControl.Invoke(New GetListboxItemInvoker(AddressOf GetListboxItem), args))
    51.         Else
    52.             text = MyControl.Items.Item(index)
    53.         End If
    54.         Return text
    55.     End Function
    56. end class
     
  3. Heya

    Heya Level III

    Joined:
    Oct 14, 2009
    Messages:
    411
    Likes Received:
    34
    check that make sure that count is coming back as 3. You also might need to declare i.

    Code (Text):
    1. 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.
     
  4. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    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):
    1. found jim
    2. not found jack
    3. not found james
    4. 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
     
  5. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    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