[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:wave:
Re: [2008] Changing ListView items on one form from another form
its probably because of the different thread.
use a delegate sub:
vb Code:
Private Delegate Sub change_listview_Callback()
Private Sub change_listview()
If listview1.InvokeRequired Then
listview1.Invoke(New change_listview_Callback(AddressOf change_listview))
Else
''put your code from your sub here
End If
End Sub
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:
Private Delegate Function ItemFoundInListInvoker(ByVal oldname As String, _
ByVal name As String, _
ByVal changes As Boolean) As Boolean
Private Function ItemFoundInList(ByVal oldname As String, _
ByVal name As String, _
ByVal changes As Boolean) As Boolean
If Me.ListView1.InvokeRequired Then
Me.ListView1.Invoke(New ItemFoundInListInvoker(AddressOf ItemFoundInList), oldname, name, changes)
Else
Dim index, itemsCnt As Integer
index = -1
itemsCnt = Me.ListView1.Items.Count - 1
For i As Integer = 0 To itemsCnt
If Me.ListView1.Items.Item(i).SubItems(3).Text = oldname Then
index = i
Exit For
End If
Next
If index = -1 Then
Return False
Else
If changes Then
Me.ImageList1.Images.Add(Icon.ExtractAssociatedIcon(name))
Me.ListView1.Items.Item(index).ImageIndex = ImageList1.Images.Count - 1
Me.ListView1.Items.Item(index).Text = name
Me.ListView1.Items.Item(index).SubItems.Item(1).Text = GetFileType(name)
Me.ListView1.Items.Item(index).SubItems.Item(3).Text = name
End If
Return True
End If
End If
End Function
Re: [2008] Changing ListView items on one form from another form
Oh come on! Somebody please HELP! I'm getting desperate ;)
Re: [2008] Changing ListView items on one form from another form
Never mind, problem SOLVED!