Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

Need Syntax help with a delegate for a listview

Friends,

I am getting the dreaded cross-threaded operation not allowed exception when I am trying to add items to my listview.  So, the simplest solution is to use a delegate.  Now, attached, you will my delegate code, except that I am having a problem with it.

If you look in here:

Me.BeginInvoke(ssd, New Object() {listViewToUpdate, ItemToAdd})

            Else
                Select Case listViewToUpdate
                    Case listViewToUpdate.lvShortCuts <-- Here I have the error:
'lvShortCuts' is not a member of 'System.Windows.Forms.ListView'. However, lvShortCuts is infact my listview.  Any idea what's wrong and how to fix it?

Thanks in advance!

Best Regards,
Eric
Private Delegate Sub AddListViewDelegate(ByVal listViewToUpdate As ListView, ByVal ItemToAdd As ListViewItem)
    Public Sub AddListViewItem(ByVal listViewToUpdate As ListView, ByVal ItemToAdd As ListViewItem)
        Try
 
 
            If Me.InvokeRequired Then
                Trace.WriteLine("UpdateScreenLabel - Invoke is required")
                Dim ssd As New AddListViewDelegate(AddressOf AddListViewItem)
 
                Me.BeginInvoke(ssd, New Object() {listViewToUpdate, ItemToAdd})
 
            Else
                Select Case listViewToUpdate
                    Case listViewToUpdate.lvShortCuts
                        lvShortCuts.Items.Add(ItemToAdd)
                End Select
            End If
 
        Catch ex As Exception
 
        End Try
 
    End Sub
 
    Enum listViewToUpdate
        lvShortCuts
    End Enum

Open in new window

0
indy500fan
Asked:
indy500fan
  • 3
  • 3
1 Solution
 
SameerJagdaleCommented:
try adding this:

Control.CheckForIllegalCrossThreadCalls = False
0
 
indy500fanAuthor Commented:
SameerJagdale,

I would rather not.  I am wanting to work on my Delagate and fix the syntax therein.

Best Regards,
Eric
0
 
SameerJagdaleCommented:
good luck :-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SameerJagdaleCommented:
BTW, what is this
Case listViewToUpdate.lvShortCuts
your listview is listViewToUpdate. right?
0
 
indy500fanAuthor Commented:
Well, the acutal name of the listview that I want to update is lvShortcuts, listViewToUpdate is where lvShortCuts is enumerated.
0
 
indy500fanAuthor Commented:
I got it.  Thanks anyway.

Here was the code I used to make it work:
    Private Delegate Sub AddListViewDelegate(ByVal listView As listViewToUpdate, ByVal ItemToAdd As String, ByVal SubItem1 As String, ByVal SubItem2 As String)
    Public Sub AddListViewItem(ByVal listView As listViewToUpdate, ByVal ItemToAdd As String, ByVal SubItem1 As String, ByVal SubItem2 As String)
        Try
 
            If Me.InvokeRequired Then
                Trace.WriteLine("UpdateScreenLabel - Invoke is required")
                Dim ssd As New AddListViewDelegate(AddressOf AddListViewItem)
 
                Me.BeginInvoke(ssd, New Object() {listView, ItemToAdd, SubItem1, SubItem2})
 
            Else
                Select Case listView
                    Case listViewToUpdate.lvShortCuts
                        Dim li As New ListViewItem
                        li = lvShortCuts.Items.Add(ItemToAdd)
                        li.SubItems.Add(SubItem1)
                        li.SubItems.Add(SubItem2)
                End Select
            End If
 
        Catch ex As Exception
 
        End Try
 
    End Sub
 
    Enum listViewToUpdate
        lvShortCuts
    End Enum

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now