"Randal Chapman" <fr******@swbell.net> wrote in message
news:82**************************@posting.google.c om...
"Chuck" <ce*****@austin.rr.com> wrote in message
news:<Hj*****************@fe2.texas.rr.com>... "Randal Chapman" <fr******@swbell.net> wrote in message
news:82**************************@posting.google.c om... > Hi.
>
> I have a custom class which inherits from
> ListViewItem.ListViewSubItem.
>
> It works fine to add new subitems to a listview, but when I try and
> get them out it tells me that it is an invalid cast. So, it let me add
> the custom item but won't let me get it back out? What's wrong here?
> Could it have something to do with the fact that the ListViewSubItem
> is a subclass?
>
> sViewSubItem = new CustListViewSubItem();
> sViewSubItem = (CustListViewSubItem)sViewItem.SubItems[i];
>
> _Randal
What type does the watch window say the subitem is? Even if a class is
cast
to a base class the watch will correctly ID the correct class.
Chuck
It's a ListViewItem.ListViewSubItem like it's supposed to be. Try it,
it just doesn't seem to work for some reason.
_Randal
Try this for a general 'catcher'
private void listView1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
ListViewItem lvi = listView1.SelectedItems[0];
for(int i=0; i< lvi.SubItems.Count; i++)
{
if(lvi.SubItems[i] is CustListViewSubItem)
{
DoCustThings();
}
else if(lvi.SubItems[i]is ListViewItem.ListViewSubItem)
{
DoBaseThings();
}
}
}
Chuck