Changing Collection Item Type

J

Johnny J.

I would like to add an extra property to the ListV iewColumns. Is that
possible. First of all, It doesn't look like there is a "Column" object at
all so what object would I inherit the Column items from?

And is it possible to "substitute" the ColumnHeaderCollection's Column items
with my inherited items (preferably with designtime support)?

If anybody can give me pointers as to how it can be done, I would appreciate
that.

Cheers,
Johnny J.
 
P

Phillip Taylor

I would like to add an extra property to the ListV iewColumns. Is that
possible. First of all, It doesn't look like there is a "Column" object at
all so what object would I inherit the Column items from?

And is it possible to "substitute" the ColumnHeaderCollection's Column items
with my inherited items (preferably with designtime support)?

If anybody can give me pointers as to how it can be done, I would appreciate
that.

Cheers,
Johnny J.

To create a class that has all the functionality of a ListViewColumn
and also has your own features you need to subclass it like this:

Public Class MyListViewColumn
Inherits ColumnHeader

End Class

Then you can add more properties to it like this:

Public Class MyListViewColumn
Inherits ColumnHeader

Private mColumnID as Int32
Public Property ColumnID() As Int32
Get
return mColumnID
End Get
Set(ByVal value As Int32)
mColumnID = value
End Set
End Property

End Class
-
Now you have a Column that has a ColumnID property you can use.

In order to actually get this into your ListView, go to the forms
designer code. (Find the form in the Solution explorer, click the plus
sign next to it and open the designer code which has a name like
Form1.designer.vb

Do a search and replace on ColumnHeader for MyListViewColumn and when
you go back and edit the form with the actual visual studio GUI
designer, your additional property should appear on that dialog box.

-
Hope this helps

Phillip Taylor.
 
J

Johnny J.

Thanks. That would probably solve it for a ListView that is designed using
the forms designer (which is good, but not the only requirement).

But will it also work if I want to add columns at runtime? I mean, when
MyListViewColumn is inherited from ColumnHeader, can I actually do:

Dim MyColumn as MyListViewColumn
MyListView.Columns.Add(MyColumn)

When the ListView is expecting a System.Windows.Forms.ColumnHeader - not a
MyNameSpace.MyListViewClass.MyListViewColumn ????

And of course, I would like to be able to edit the columns collection at
designtime using the Collection editor with MyListViewColumns instead of
ColumnHeaders???

Cheers,
Johnny J.
 
P

Phillip Taylor

Thanks. That would probably solve it for a ListView that is designed using
the forms designer (which is good, but not the only requirement).

But will it also work if I want to add columns at runtime? I mean, when
MyListViewColumn is inherited from ColumnHeader, can I actually do:

Dim MyColumn as MyListViewColumn
MyListView.Columns.Add(MyColumn)

When the ListView is expecting a System.Windows.Forms.ColumnHeader - not a
MyNameSpace.MyListViewClass.MyListViewColumn ????

And of course, I would like to be able to edit the columns collection at
designtime using the Collection editor with MyListViewColumns instead of
ColumnHeaders???

Cheers,
Johnny J.

Yes you can do MyListView.Columns.Add because it's expecting a
ColumnHeader and you ARE giving it a column header. MyListViewColumn
inherits from ColumnHeader. It is a _type_ of ColumnHeader, just like
a LinkedList asks for an "object" and you can provide anything that
derives from it, you can treat this exactly like a columnheader. It
has the same functions, properties etc. This feature is called
Inheritence and it's a benefit of Object-Oriented programming. Perhaps
you may be a little more confident if you read up more.

How here's something to be aware of. You give a MyColumnHeader which
happily goes into a MyListView.Columns, but that collection will
always be a type of "HeaderColumnCollection". The list view itself is
unaware of the extra data. (It doesn't get lost or forgotten) but when
you come to access it like this:

MyListView.Columns(0) 'first column header.

This will be a type *considered* a type of ColumnHeader, not
MyColumnHeader. You will need to explicitly "cast" the object back
into the correct type you know that it is:

dim myColumn As MyColumnHeader =
directcast(mylistview.Columns(0),MyColumnHeader)

First, on the right hand side of the equals, I explicitly say the
record at Column(0) is a MyColumnHeader. Then I assign it to myColumn.

if you have MyColumnHeaders and regular ColumnHeaders all mixed up you
will need to test it first. This sets the "ColumnID" property we made
earlier:

if (typeof mylistview.columns(0) is MyColumnHeader) then
'this is my special column header, set the column id
directcast(mylistview.columns(0).ColumnID = 78
else
'there is no column id on this column header because it's just
a regular plain old one
end if

-
Anyway hope this helps some more. Reading a little about Object
Inheritance may help you if you don't already know it.
 
J

Johnny J.

Thanks Philip.

I noticed that when I tested it out after your last mail, and that's all
well and fine.

My idea was that I would modify the ColumnHeader object with extra
properties and use them in the ListView like a "normal" ColumnHeader object
with all that that entails, including designtime design using the
collections editor from the grid.

The problem is that I don't know if there is any way of overriding the
ColumnHeader items in the ColumnHeaderCollection.

I could of course implement a new ColumnHeaderCollection type and override
the Columns property on the GridView, but that would mean that I have to
implement all of the methods of the "real" ColumnHeaderCollection to get the
exact same functionality as the normal listview - plus all the serialization
problems that might entail...

I would rather not do that, but I'll try if I absolutely have to. I guess I
was just hoping there was an easier way.

Cheers,
Johnny J.
 

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