Results 1 to 5 of 5

Thread: [RESOLVED] [2008] Changing ListView items on one form from another form

  1. #1

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Resolved [RESOLVED] [2008] Changing ListView items on one form from another form

    Ok, basically, what I tried to do is:
    I have two forms, the app's main form and a second form.
    When a particular event happens on the second form, I want to change an item in a ListView on the main form.
    Since events cannot be raised from another class, I created a Public Sub in the main form with all the necessary code to change the ListViewItem in the ListView. I then call the sub from a background worker thread of the second form, with all the neccessary Invokes and stuff.

    Now, to the problem:
    The sub accesses the ListView in its own class, but refuses to view any of its items or the Items.Count property, and is therefore useless.

    Why is this happening and how do I solve it? Also, if there is another, better way of doing this, please tell me. Thx

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008] Changing ListView items on one form from another form

    its probably because of the different thread.
    use a delegate sub:

    vb Code:
    1. Private Delegate Sub change_listview_Callback()
    2.  
    3. Private Sub change_listview()
    4.     If listview1.InvokeRequired Then
    5.         listview1.Invoke(New change_listview_Callback(AddressOf change_listview))
    6.     Else
    7.         ''put your code from your sub here
    8.     End If
    9. End Sub

  3. #3

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Changing ListView items on one form from another form

    I am already using a delegate sub, so that's not the answer. Here's the actual code, if that's any help:
    vb.net Code:
    1. Private Delegate Function ItemFoundInListInvoker(ByVal oldname As String, _
    2.                                                      ByVal name As String, _
    3.                                                      ByVal changes As Boolean) As Boolean
    4.  
    5. Private Function ItemFoundInList(ByVal oldname As String, _
    6.                                      ByVal name As String, _
    7.                                      ByVal changes As Boolean) As Boolean
    8.         If Me.ListView1.InvokeRequired Then
    9.             Me.ListView1.Invoke(New ItemFoundInListInvoker(AddressOf ItemFoundInList), oldname, name, changes)
    10.         Else
    11.             Dim index, itemsCnt As Integer
    12.             index = -1
    13.             itemsCnt = Me.ListView1.Items.Count - 1
    14.             For i As Integer = 0 To itemsCnt
    15.                 If Me.ListView1.Items.Item(i).SubItems(3).Text = oldname Then
    16.                     index = i
    17.                     Exit For
    18.                 End If
    19.             Next
    20.             If index = -1 Then
    21.                 Return False
    22.             Else
    23.                 If changes Then
    24.                     Me.ImageList1.Images.Add(Icon.ExtractAssociatedIcon(name))
    25.                     Me.ListView1.Items.Item(index).ImageIndex = ImageList1.Images.Count - 1
    26.                     Me.ListView1.Items.Item(index).Text = name
    27.                     Me.ListView1.Items.Item(index).SubItems.Item(1).Text = GetFileType(name)
    28.                     Me.ListView1.Items.Item(index).SubItems.Item(3).Text = name
    29.                 End If
    30.                 Return True
    31.             End If
    32.         End If
    33.     End Function

  4. #4

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Changing ListView items on one form from another form

    Oh come on! Somebody please HELP! I'm getting desperate

  5. #5

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Changing ListView items on one form from another form

    Never mind, problem SOLVED!
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

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