Inherited ListView

  • Thread starter Thread starter David Gouge
  • Start date Start date
D

David Gouge

Hi all,

Hope this is a simple one for you but i seem to be having a bit of a
mental block.

I have an inherited ListView (NewListView) class and an inherited
ListViewItem (NewListViewItem) class. How do i get my NewListView class
to accept NewListViewItems into and return NewListViewItems from the
Items collection.

I've got a feeling that it's really quite straight forward. Just can't
see it right now.

Cheers.
 
David said:
I have an inherited ListView (NewListView) class and
an inherited ListViewItem (NewListViewItem) class.
How do i get my NewListView class to accept
NewListViewItems into and return NewListViewItems
from the Items collection.

You can already add a NewListViewItem to a ListViewItemCollection,
because it's a suclass of ListViewItem. If you want to return them as
well, or do anything meaningful with the parts that are only available
to in the subclass, you'll have to override lots of things in the base
class, and probably write your own NewListViewItemCollection.

Eq.
 
Paul said:
You can already add a NewListViewItem to a ListViewItemCollection,
because it's a suclass of ListViewItem. If you want to return them as
well, or do anything meaningful with the parts that are only available
to in the subclass, you'll have to override lots of things in the base
class, and probably write your own NewListViewItemCollection.

Eq.

Cheers for the reply Paul.

Yeah, that's basically where i got with it. As you say, they add in ok,
but then i have to cast them back out and can't access the new
properties 'inside'.

Any pointers as to 'overriding lots of things' to get it to work? ;)

Cheers!

dave
 
David said:
Any pointers as to 'overriding lots of things' to get it to work? ;)

Afraid not. I *hate* overriding things whose source code I can't see,
and try to avoid it. I'd probably use a regular ListView (with custom
paint code if you need to draw something special) and store the
metadata in an ArrayList or something, ensuring it's kept synchronised
with the control's contents. Nasty hack, but might save days of work
only to find that the ListView can't be inherited how you intended
(sometimes they don't expose all the things required).

Eq.
 
Afraid not. I *hate* overriding things whose source code I can't see, and
try to avoid it. I'd probably use a regular ListView (with custom paint
code if you need to draw something special) and store the metadata in an
ArrayList or something, ensuring it's kept synchronised with the control's
contents. Nasty hack, but might save days of work only to find that the
ListView can't be inherited how you intended (sometimes they don't expose
all the things required).

I'd agree with Paul here, when trying to 'override lots of things' to get it
to work, you'll often end up with overriding almost "everything"...

An alternative to Paul's suggestion of using a "regular" ListView, is
another approach I've often used.

In order to create a public custom control from a pre-built one, I often use
Composition instead of, or in combination with Inheritance, e.g. to put a
ListView onto a UserControl, instead of directly inherit from the ListView.
That way you have better control over what is exposed of your control, e.g.
what type of Items it should accept and return.

/// Bjorn A
 
Bjorn said:
...

I'd agree with Paul here, when trying to 'override lots of things' to get it
to work, you'll often end up with overriding almost "everything"...

An alternative to Paul's suggestion of using a "regular" ListView, is
another approach I've often used.

In order to create a public custom control from a pre-built one, I often use
Composition instead of, or in combination with Inheritance, e.g. to put a
ListView onto a UserControl, instead of directly inherit from the ListView.
That way you have better control over what is exposed of your control, e.g.
what type of Items it should accept and return.

Thanks to you both for the replies. I think i could hear the sharp
intake of breath from here! ;)
 
Back
Top