Answered by:
Listview control exception

Question
-
Hi everyone,
I hope someone can help me with this as I was unable to find anything on my own.
I have a C# very basic GUI application with 2 buttons and a listview control.
The listview control is being populated by a thread that is started from an external c++ dll I made myself. I pass the handle to the DLL function and I add items this way :
LVITEM lvItem; lvItem.mask = LVIF_TEXT; lvItem.iItem = 0; lvItem.iSubItem = 0; lvItem.pszText = "Just another entry"; lvItem.iItem = (int) SendMessage( hList, LVM_GETITEMCOUNT, 0, 0); SendMessage( hList, LVM_INSERTITEM, 0, (LPARAM) &lvItem);
This works fine and the listview gets populated with no errors.
However if I give focus to the listview and maybe select different items or take the focus away I receive the following Exception :
************** Exception Text **************
System.ArgumentOutOfRangeException: InvalidArgument=Value of '2' is not valid for 'index'.
Parameter name: index
at System.Windows.Forms.ListView.ListViewItemCollection.get_Item(Int32 index)
at System.Windows.Forms.ListView.get_FocusedItem()
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
HTTPLister
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/Code/HTTPLister/HTTPLister/bin/Release/HTTPLister.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
I do not have any code related to the the listview control on the C# side so I assume everything is handled by the default proc for the control.
I could really use some help with this.
Thanks in advanceSunday, December 14, 2008 2:57 PM
Answers
-
Restructure your code so the unmanaged code just supplies the data. Let the managed code take care of the UI.
Hans Passant.- Marked as answer by nobugz Saturday, January 3, 2009 7:08 PM
Friday, December 19, 2008 11:56 AM
All replies
-
You cannot use a thread to do anything with a control. All code that touches properties or sends messages must run on the same thread that created the control. Invariably, that's the UI thread. Windows Forms normally generates an InvalidOperation exception when you try, but that can't work when you use unmanaged C++ code to change the item collection.
In addition, the WF ListView control uses custom painting and relies on its Items collection to provide the item data. You added an item to the control without using the Items collection.
Hans Passant.Sunday, December 14, 2008 3:19 PM -
Alright, thanks, it does make sense.
How do you think I could get the listening thread to pass the information to the GUI to be displayed in realtime (or seemingly?)
I appreciate the help.
I am really just using .NET to make the GUI easier to create and manage as it's not my main focus.
--
JeremySunday, December 14, 2008 4:11 PM -
Restructure your code so the unmanaged code just supplies the data. Let the managed code take care of the UI.
Hans Passant.- Marked as answer by nobugz Saturday, January 3, 2009 7:08 PM
Friday, December 19, 2008 11:56 AM