Bugs with virtual list-view

  • Thread starter Thread starter Kevin Westhead
  • Start date Start date
K

Kevin Westhead

I'm working on a virtual list-view control derived from
System.Windows.Forms.ListView, however I've been running into problems
because the base control thinks it owns the items in the list. For example,
I get the following exception whenever the control receives focus:

Specified argument was out of the range of valid values.
Parameter name: '5' is not a valid value for 'displayIndex'.
at System.Windows.Forms.ListViewItemCollection.get_Item(Int32
displayIndex)
at System.Windows.Forms.ListView.get_FocusedItem()
at System.Windows.Forms.ListView.WndProc(Message& m)
at VListView.WndProc(Message& m) in ..\listview.cs:line 355

The code for the base control, in response to WM_SETFOCUS, is something like
this:

base.WndProc(m);

if ((this.FocusedItem != null) || (this.Items.Count <= 0))
return;

this.Items[0].Focused = true;
return;

The FocusedItem property is as follows:

int num1;

if (base.IsHandleCreated)
{
num1 = base.SendMessage(4108, -1, 1); // LVM_GETNEXTITEM

if (num1 > -1)
return this.Items[num1];

}

return null;

It looks to me as though 'this.Items.Count' should be tested first in
response to WM_SETFOCUS in order to prevent the control from trying to
retrieve an item from a non-existent collection. My workaround is to call
'base.DefWndProc' in the derived control in response to WM_SETFOCUS. I have
also come across problems in OnHandleDestroyed, which again seems to assume
that if there is a selection then there must also be a valid
ListViewItemCollection.

Has anyone else run into these problems? Is it just a case of working around
the assumptions made in System.Windows.Forms.ListView or am I missing a step
when setting up my virtual list-view that would avoid these problems?

TIA.
 
Hi Kevin,
Why not try implementing the DataBinding on ListView, Here is an
article, I hope it will help you somehow.
http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet08262002.asp?fram
e=true
And about you problem, have you setup the right style when creating the
control?,
you may search LVS OWNERDATA in MSDN for detailed information.
and here is also a discussion thread on your approach
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=0dhn9.64
92%24s5.193245%40twister.southeast.rr.com&rnum=1&prev=/groups%3Fq%3D%2522Lio
n%2BShi%2522%2BVirtual%2BListView%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-
8%26selm%3D0dhn9.6492%2524s

Is the information above helpful to your problem?
If you still have problems on this issue, please let me know,
Thanks!


Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Kevin Westhead" <[email protected]>
| Subject: Bugs with virtual list-view
| Date: Thu, 4 Sep 2003 16:10:14 +0100
| Lines: 57
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: host5.i2.co.uk 193.129.24.5
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA0
5.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51636
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I'm working on a virtual list-view control derived from
| System.Windows.Forms.ListView, however I've been running into problems
| because the base control thinks it owns the items in the list. For
example,
| I get the following exception whenever the control receives focus:
|
| Specified argument was out of the range of valid values.
| Parameter name: '5' is not a valid value for 'displayIndex'.
| at System.Windows.Forms.ListViewItemCollection.get_Item(Int32
| displayIndex)
| at System.Windows.Forms.ListView.get_FocusedItem()
| at System.Windows.Forms.ListView.WndProc(Message& m)
| at VListView.WndProc(Message& m) in ..\listview.cs:line 355
|
| The code for the base control, in response to WM_SETFOCUS, is something
like
| this:
|
| base.WndProc(m);
|
| if ((this.FocusedItem != null) || (this.Items.Count <= 0))
| return;
|
| this.Items[0].Focused = true;
| return;
|
| The FocusedItem property is as follows:
|
| int num1;
|
| if (base.IsHandleCreated)
| {
| num1 = base.SendMessage(4108, -1, 1); // LVM_GETNEXTITEM
|
| if (num1 > -1)
| return this.Items[num1];
|
| }
|
| return null;
|
| It looks to me as though 'this.Items.Count' should be tested first in
| response to WM_SETFOCUS in order to prevent the control from trying to
| retrieve an item from a non-existent collection. My workaround is to call
| 'base.DefWndProc' in the derived control in response to WM_SETFOCUS. I
have
| also come across problems in OnHandleDestroyed, which again seems to
assume
| that if there is a selection then there must also be a valid
| ListViewItemCollection.
|
| Has anyone else run into these problems? Is it just a case of working
around
| the assumptions made in System.Windows.Forms.ListView or am I missing a
step
| when setting up my virtual list-view that would avoid these problems?
|
| TIA.
|
| --
| Kevin Westhead
|
|
|
 
Hi Kevin,
Here is an example for Virtual List View.
http://www.windowsforms.net/ControlGallery/ControlDetail.aspx?Control=214&ta
bindex=9
You may try it to see if it meets your need, thanks!


Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| X-Tomcat-ID: 383996803
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Ying-Shen Yu[MS])
| Organization: Microsoft
| Date: Fri, 05 Sep 2003 15:05:52 GMT
| Subject: RE: Bugs with virtual list-view
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Lines: 96
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51745
| NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182
|
| Hi Kevin,
| Why not try implementing the DataBinding on ListView, Here is an
| article, I hope it will help you somehow.
|
http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet08262002.asp?fram
| e=true
| And about you problem, have you setup the right style when creating the
| control?,
| you may search LVS OWNERDATA in MSDN for detailed information.
| and here is also a discussion thread on your approach
|
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=0dhn9.64
|
92%24s5.193245%40twister.southeast.rr.com&rnum=1&prev=/groups%3Fq%3D%2522Lio
|
n%2BShi%2522%2BVirtual%2BListView%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-
| 8%26selm%3D0dhn9.6492%2524s
|
| Is the information above helpful to your problem?
| If you still have problems on this issue, please let me know,
| Thanks!
|
|
| Kind regards,
|
| Ying-Shen Yu [MS]
| Microsoft Support Engineer
|
| This posting is provided "AS IS" with no warranties, and confers no
rights.
| You assume all risk for your use. 2001 Microsoft Corporation. All rights
| reserved.
| --------------------
| | From: "Kevin Westhead" <[email protected]>
| | Subject: Bugs with virtual list-view
| | Date: Thu, 4 Sep 2003 16:10:14 +0100
| | Lines: 57
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | Message-ID: <[email protected]>
| | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| | NNTP-Posting-Host: host5.i2.co.uk 193.129.24.5
| | Path:
|
cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA0
| 5.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.framework.windowsforms:51636
| | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| |
| | I'm working on a virtual list-view control derived from
| | System.Windows.Forms.ListView, however I've been running into problems
| | because the base control thinks it owns the items in the list. For
| example,
| | I get the following exception whenever the control receives focus:
| |
| | Specified argument was out of the range of valid values.
| | Parameter name: '5' is not a valid value for 'displayIndex'.
| | at System.Windows.Forms.ListViewItemCollection.get_Item(Int32
| | displayIndex)
| | at System.Windows.Forms.ListView.get_FocusedItem()
| | at System.Windows.Forms.ListView.WndProc(Message& m)
| | at VListView.WndProc(Message& m) in ..\listview.cs:line 355
| |
| | The code for the base control, in response to WM_SETFOCUS, is something
| like
| | this:
| |
| | base.WndProc(m);
| |
| | if ((this.FocusedItem != null) || (this.Items.Count <= 0))
| | return;
| |
| | this.Items[0].Focused = true;
| | return;
| |
| | The FocusedItem property is as follows:
| |
| | int num1;
| |
| | if (base.IsHandleCreated)
| | {
| | num1 = base.SendMessage(4108, -1, 1); // LVM_GETNEXTITEM
| |
| | if (num1 > -1)
| | return this.Items[num1];
| |
| | }
| |
| | return null;
| |
| | It looks to me as though 'this.Items.Count' should be tested first in
| | response to WM_SETFOCUS in order to prevent the control from trying to
| | retrieve an item from a non-existent collection. My workaround is to
call
| | 'base.DefWndProc' in the derived control in response to WM_SETFOCUS. I
| have
| | also come across problems in OnHandleDestroyed, which again seems to
| assume
| | that if there is a selection then there must also be a valid
| | ListViewItemCollection.
| |
| | Has anyone else run into these problems? Is it just a case of working
| around
| | the assumptions made in System.Windows.Forms.ListView or am I missing a
| step
| | when setting up my virtual list-view that would avoid these problems?
| |
| | TIA.
| |
| | --
| | Kevin Westhead
| |
| |
| |
|
|
 
Ying-Shen Yu said:
Hi Kevin,
Why not try implementing the DataBinding on ListView, Here is an
article, I hope it will help you somehow.
http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet08262002.asp?fram
e=true
And about you problem, have you setup the right style when creating the
control?,
you may search LVS OWNERDATA in MSDN for detailed information.
and here is also a discussion thread on your approach
http://groups.google.com/groups?hl=...2+Virtual+ListView&hl=en&lr=&ie=UTF-8&oe=UTF-
8%26selm%3D0dhn9.6492%2524s


My understanding of DataBinding, which I admit isn't great, is that the
ListView will still own the data, i.e. the data is still physically loaded
into the control it's just that the process of getting the data into the
control is simplified. I want to use a virtual list-view to by-pass the
process of adding data to the control altogether, i.e. the application owns
the data and the list-view just asks for the text that it will display.

I'm overriding CreateParams as follows in order to make my list-view
virtual:

protected override CreateParams CreateParams
{

get
{
CreateParams cp = base.CreateParams;
cp.Style |= (int)ListViewStyles.OwnerData;
return cp;
}

}

This is working, it's just that I'm running into problems as mentioned
previously because the underlying System.Windows.Forms.ListView control
doesn't seem to be aware that it's virtual.
 
Ying-Shen Yu said:
Hi Kevin,
Here is an example for Virtual List View.
http://www.windowsforms.net/ControlGallery/ControlDetail.aspx?Control=214&ta
bindex=9

Thanks for the link, but unfortunately this implementation demonstrates the
same issues that I described. To see it all you have to do is place a button
on the demo Form, rebuild and run. Click the button so that it takes focus
from the list-view, then select an item in the list-view to give focus back.
An unhandled exception occurs due to the FocusedItem problem I described.
Restart, then without clicking the button (the list-view should already have
focus) select an item and close the application. Another unhandled exception
occurs due the the OnHandleDestroyed problem I described. I have since
discovered that LVN_BEGINDRAG and LVN_BEGINRDRAG also cause exceptions due
to how the LvnBeginDrag method, which is called in response to these
notifications, is implemented in System.Windows.Forms.ListView:

private void LvnBeginDrag(MouseButtons buttons, NMLISTVIEW nmlv)
{
ListViewItem item1;
item1 = this.Items[nmlv.iItem];
this.OnItemDrag(new ItemDragEventArgs(buttons, item1));
}

Note that in a virtual list-view, the Items collection will always be empty
hence an ArgumentOutOfRangeException occurs.
 
Hi Kevin,
I can repro this problem and sent a mail to the maker of that sample.
I'll inform you if I got the reply from him.
And I also investigate into the listview for some time. It seems you are
right, it seems very hard to change the default behaviour of the ListView,
but I wonder if it could be fixed by handling more message by yourself.
And that google thread(refer to my last post), Gary seems having done a
workable Virtual ListView. he derived his ListView from the ListBox instead
of the ListView, and forced to create a ListView in CreateParams, Maybe You
can try this way, but you need implement almost all by yourself.
If you have any further questions or ideas ,please let me know.
Thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Kevin Westhead" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Bugs with virtual list-view
| Date: Fri, 5 Sep 2003 16:54:17 +0100
| Lines: 51
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: host5.i2.co.uk 193.129.24.5
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51752
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| | > Hi Kevin,
| > Why not try implementing the DataBinding on ListView, Here is an
| > article, I hope it will help you somehow.
| >
|
http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet08262002.asp?fram
| > e=true
| > And about you problem, have you setup the right style when creating the
| > control?,
| > you may search LVS OWNERDATA in MSDN for detailed information.
| > and here is also a discussion thread on your approach
| >
|
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=0dhn9.64
| >
|
92%24s5.193245%40twister.southeast.rr.com&rnum=1&prev=/groups%3Fq%3D%2522Lio
| >
|
n%2BShi%2522%2BVirtual%2BListView%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-
| > 8%26selm%3D0dhn9.6492%2524s
|
|
| My understanding of DataBinding, which I admit isn't great, is that the
| ListView will still own the data, i.e. the data is still physically loaded
| into the control it's just that the process of getting the data into the
| control is simplified. I want to use a virtual list-view to by-pass the
| process of adding data to the control altogether, i.e. the application
owns
| the data and the list-view just asks for the text that it will display.
|
| I'm overriding CreateParams as follows in order to make my list-view
| virtual:
|
| protected override CreateParams CreateParams
| {
|
| get
| {
| CreateParams cp = base.CreateParams;
| cp.Style |= (int)ListViewStyles.OwnerData;
| return cp;
| }
|
| }
|
| This is working, it's just that I'm running into problems as mentioned
| previously because the underlying System.Windows.Forms.ListView control
| doesn't seem to be aware that it's virtual.
|
| --
| Kevin Westhead
|
|
|
 
Ying-Shen Yu said:
I can repro this problem and sent a mail to the maker of that sample.
I'll inform you if I got the reply from him.
And I also investigate into the listview for some time. It seems you are
right, it seems very hard to change the default behaviour of the ListView,
but I wonder if it could be fixed by handling more message by yourself.
And that google thread(refer to my last post), Gary seems having done a
workable Virtual ListView. he derived his ListView from the ListBox instead
of the ListView, and forced to create a ListView in CreateParams, Maybe You
can try this way, but you need implement almost all by yourself.
If you have any further questions or ideas ,please let me know.
Thanks!


Yes, I think it's just a case of working around the problems as and when
they occur; my concern was that I was missing some simple flag on the
control that would have correctly set it up for owner-data mode. In an ideal
world I would create my own list-view control, derived from Control rather
than deriving from ListView, in order to avoid these issues. Unfortunately I
don't have the time to implement all of the additional functionality of the
list-view so I'll carry on working around the problems. It's fairly stable
now, although I don't really have a solution to the OnHandleDestroyed
problem other than to eat the exception, which concerns me because there's a
lot of code in Control.OnHandleDestroyed that will get skipped.
 
Hi Kevin,

| Yes, I think it's just a case of working around the problems as and when
| they occur; my concern was that I was missing some simple flag on the
| control that would have correctly set it up for owner-data mode. In an
ideal
| world I would create my own list-view control, derived from Control rather
| than deriving from ListView, in order to avoid these issues.
Unfortunately I
| don't have the time to implement all of the additional functionality of
the
| list-view so I'll carry on working around the problems. It's fairly stable
| now, although I don't really have a solution to the OnHandleDestroyed
| problem other than to eat the exception, which concerns me because
there's a
| lot of code in Control.OnHandleDestroyed that will get skipped.
|
| --
| Kevin Westhead
Thank you for your feedback, we will try our best to improve the listview
and make it easier to extend. On your question, I think it's ok to swallow
the exception in OnHandleDestroyed, since you didn't use the Item property.
All the other unmanaged resources (Window Handle, GDI Object, etc) will be
freed in Dispose method. And could you share you work around with all other
community users?

Thank you very much!


Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer
 
Ying-Shen Yu said:
Thank you for your feedback, we will try our best to improve the listview
and make it easier to extend. On your question, I think it's ok to swallow
the exception in OnHandleDestroyed, since you didn't use the Item property.
All the other unmanaged resources (Window Handle, GDI Object, etc) will be
freed in Dispose method. And could you share you work around with all other
community users?


I might write a Code Project article about this, although I want to sit on
the code for a little longer to make sure that it's reasonably robust.
 
Hi kevin,
I got a good news that If everything is all right, we will have the
virtualization support on ListView in next VS.NET release.
Thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Kevin Westhead" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: Bugs with virtual list-view
| Date: Tue, 9 Sep 2003 21:58:36 +0100
| Lines: 20
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: pc4-cmbg2-4-cust221.cmbg.cable.ntl.com 80.6.126.221
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51978
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| | > Thank you for your feedback, we will try our best to improve the
| listview
| > and make it easier to extend. On your question, I think it's ok to
swallow
| > the exception in OnHandleDestroyed, since you didn't use the Item
| property.
| > All the other unmanaged resources (Window Handle, GDI Object, etc) will
be
| > freed in Dispose method. And could you share you work around with all
| other
| > community users?
|
|
| I might write a Code Project article about this, although I want to sit on
| the code for a little longer to make sure that it's reasonably robust.
|
| --
| Kevin Westhead
|
|
|
 
Back
Top