Solved

Need Syntax help with a delegate for a listview

Posted on 2008-10-08
6
533 Views
Last Modified: 2012-08-14
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
Comment
Question by:indy500fan
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22668602
try adding this:

Control.CheckForIllegalCrossThreadCalls = False
0
 

Author Comment

by:indy500fan
ID: 22668927
SameerJagdale,

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

Best Regards,
Eric
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22669021
good luck :-)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22669034
BTW, what is this
Case listViewToUpdate.lvShortCuts
your listview is listViewToUpdate. right?
0
 

Author Comment

by:indy500fan
ID: 22669057
Well, the acutal name of the listview that I want to update is lvShortcuts, listViewToUpdate is where lvShortCuts is enumerated.
0
 

Accepted Solution

by:
indy500fan earned 0 total points
ID: 22669217
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 46
Allow user to hide and show datagridview columns 4 25
Point to Current Row Ater Refresh Datagridview 3 24
Disable extension 8 38
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question