Cannot Cast from ListViewSubItem

  • Thread starter Thread starter Randal Chapman
  • Start date Start date
R

Randal Chapman

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;

_Randal
 
Randal Chapman said:
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;

_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
 
Chuck said:
Randal Chapman said:
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;

_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
 
Randal Chapman said:
Chuck said:
Randal Chapman said:
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;

_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


It worked fine for me, BUT only after I remembered that the first sub-item
is index 1 and NOT index 0 (which actually returns the item that owns the
subitems). If i has the value 0 in your example code then that explains the
invalid cast exception.

Chris Jobson
 
Randal Chapman said:
Chuck said:
Randal Chapman said:
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;

_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 is CustListViewSubItem)
{
DoCustThings();
}
else if(lvi.SubItemsis ListViewItem.ListViewSubItem)
{
DoBaseThings();
}
}
}

Chuck
 
Chris Jobson said:
It worked fine for me, BUT only after I remembered that the first sub-item
is index 1 and NOT index 0 (which actually returns the item that owns the
subitems). If i has the value 0 in your example code then that explains the
invalid cast exception.

Chris Jobson

DUH!!! I cannot believe I didn't think of that! Thanks man, it works fine now.

_Randal
 
Back
Top