473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cannot Cast from ListViewSubItem

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
Nov 16 '05 #1
5 2552

"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
Nov 16 '05 #2
"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
Nov 16 '05 #3
"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


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
Nov 16 '05 #4

"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
Nov 16 '05 #5
"Chris Jobson" <ch**********@btinternet.com> wrote in message news:<ea**************@TK2MSFTNGP15.phx.gbl>...

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
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: BashiraInTrouble | last post by:
Hi Friends, I have tried almost everything but I cant seem to shrink the transaction log. Executing DBCC SQLPERF(LOGSPACE) gives me this info: Database Log Size (MB) Log Space Used (%) ...
2
by: Abhijit Bhadra | last post by:
Hi , I was trying to build my project in VC with latest Service Packs but got this error . C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE\atlconv.h(125) : error C2440: 'return' :...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
1
by: Svyatoslav | last post by:
Hi, I have a problem with XmlNodes and my stack. It looks something like this: //declarations XmlNode node, new_node; Stack MyStack = new Stack(); //code MyStack.Push(node);
3
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has...
0
by: Karsten Sosna | last post by:
Hallo NG, I have a problem with the ListView-Control. I add a ListViewItem(LVI) with a ListViewSubItem(LVSI_Status) to my ListView(ListView1). Then i want to change the the Value of the...
1
by: Laurence Bunnage | last post by:
Has anyone come across the following problem and does anybody know of a solution. I am using a ListView to display the age of a collection of items. When the items are an hour old, I am using...
7
by: DippyDog | last post by:
My problem is the subject line. Basically, my thinking was that, given class TestSpec1 inherits from class TestBase, since I can do this... Dim tst As TestBase = GetTestObj() Function...
3
by: Klaudiusz Bryja | last post by:
Hi, I have some problem. I try to get ListViewSubItem type using: Type t = (new Form).GetType().Assembly.GetType("System.Windows.Forms.ListViewItem.ListViewSubItem") but I get t == null. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.