Windows Develop Bookmark and Share   
 index > Windows Forms General > ListView problem in virtual mode
 

ListView problem in virtual mode

Hi,

I have a problem with the ListView in virtual mode. On my form I've got a TreeView and a TabControl. The TabControl has 2 TabPages. Each TabPage has a ListView. Sometimes (not all the times) when I switch from one page to another and populate the listview I receive the following error in Program.cs at Application.Run(new form()); :

Code Snippet

System.InvalidOperationException was unhandled
Message="When in VirtualMode the ListView RetrieveVirtualListItem event needs a list view SubItem for each ListView column."
Source="System.Windows.Forms"

StackTrace:
at System.Windows.Forms.ListView.WmReflectNotify(Message& m)
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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmNotify(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication1.Program.Main() in C:\Documents and Settings\Paul Mutiu\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


This is the code where I deal with listview and virtual mode:

Code Snippet

private void QueryResults(WorkItemStore store, WorkItemCollection items, ListView list)
{
try
{
m_items = items;
// populate list
DisplayFieldList fieldList = m_items.Query.DisplayFieldList;
m_columns = new string[fieldList.Count];

list.Columns.Clear();
for (int i = 0; i < fieldList.Count; i++)
{
FieldDefinition field = fieldList[i];
list.Columns.Add(field.Name, 100, HorizontalAlignment.Left);
m_columns[i] = field.ReferenceName;
}

list.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(this.OnRetrieveVirtualItem);
list.VirtualMode = true;
list.VirtualListSize = m_items.Count;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}



Code Snippet

void OnRetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
if (e.ItemIndex >= m_items.Count)
{
return;
}

WorkItem item = m_items[e.ItemIndex];
string[] subItems = new string[m_columns.Length - 1];
string name = "";

for (int i = 0; i < m_columns.Length; i++)
{
object o = item[m_columns[i]];

string s;
if (o is DateTime)
{
s = ((DateTime)o).ToLocalTime().ToString();
}
else if (o != null)
{
s = o.ToString();
}
else
{
s = "<null>";
}

if (i == 0)
name = s;
else
subItems[i - 1] = s;
}

ListViewItem li = new ListViewItem(name);
li.SubItems.AddRange(subItems);
e.Item = li;
}



Code is part taken from the VS 2005 SDK example with WorkItemTypeExplorer.

Please let me know what I'm doing wrong.
Thanks,
Paul
PaulGIB  Wednesday, April 25, 2007 10:32 AM
Changing the VirtualMode property requires WF to recreate the Windows listview control. I can imagine things going wrong when you switch to virtual mode where previously there were more items. Try swapping the VirtualMode and VirtualListSize property assignments. In your test on the e.ItemIndex value, throw an exception instead of just returning so you have an idea what might be happening...
nobugz  Wednesday, April 25, 2007 12:00 PM
What do you mean by "Try swapping the VirtualMode and VirtualListSize property assignments." ? I'm using 3 listview's, can't I manage each separately? I tried doing another QueryResults function that used other handler but I still got the same error.
PaulGIB  Wednesday, April 25, 2007 12:44 PM
Change your code like this:

list.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(this.OnRetrieveVirtualItem);
list.VirtualListSize = m_items.Count;
list.VirtualMode = true;

nobugz  Wednesday, April 25, 2007 1:01 PM
stupid me i didn't realise what you were trying to say by swapping . did that, but unfortunatelly still the same error..
PaulGIB  Wednesday, April 25, 2007 1:21 PM
Getting or setting the VirtualListSize property when VirtualMode set to false will have no effect on the ListView.
PaulGIB  Friday, April 27, 2007 10:49 AM

You can use google to search for other answers

Custom Search

More Threads

• Event handler for textbox display
• C++/CLI: ResX: Retreive an Image
• Windows mms server on server 2003
• Word Toolbar while navigating in web browser control
• Viewing inbuilt functions definition!
• replace method acting strange?!?!
• Split Container User Control split borders invisible
• Timer Timer Questions
• FAQ
• Printing setting question