Cannot Cast from ListViewSubItem

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
 
C

Chuck

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
 
R

Randal Chapman

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
 
C

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


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
 
C

Chuck

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
 
R

Randal Chapman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top