By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Override Listview property method

P: n/a
I am a newbie to C# and am having trouble trying to override a ListView
property method. I have created a new class derived from the Forms.Listview
and I cannot figure out the syntax to override ListView.Items.Add(), . I see
that it is a virtual method so it should be easy to do. If anyone can help I
would appreciate it greatly. I can do what I need to do in a different way
this would just make everything significantly cleaner and eaasier to
maintain.
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Where is it exactly that you are having troubles with it?
"Dave Y" <me@work.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
I am a newbie to C# and am having trouble trying to override a ListView
property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override ListView.Items.Add(), . I see that it is a virtual method so it should be easy to do. If anyone can help I would appreciate it greatly. I can do what I need to do in a different way
this would just make everything significantly cleaner and eaasier to
maintain.

Nov 16 '05 #2

P: n/a
I'm sorry, that last question probably wasn't too helpful,
but if you're overriding a virtual method, just make sure you include the
"override" key word.
But it is important to note that the ListView.Items.Add() method is not in
the ListView class itself. I do not know the implementation for Items - but
it is probably an ArrayList or something like that.
"Jack Smash" <so*****@somewebsite.com> wrote in message
news:Oj**************@TK2MSFTNGP11.phx.gbl...
Where is it exactly that you are having troubles with it?
"Dave Y" <me@work.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
I am a newbie to C# and am having trouble trying to override a ListView
property method. I have created a new class derived from the Forms.Listview
and I cannot figure out the syntax to override ListView.Items.Add(), . I

see
that it is a virtual method so it should be easy to do. If anyone can help I
would appreciate it greatly. I can do what I need to do in a different

way this would just make everything significantly cleaner and eaasier to
maintain.


Nov 16 '05 #3

P: n/a

"Dave Y" wrote...
I am a newbie to C# and am having trouble
trying to override a ListView property method.
I have created a new class derived from the
Forms.Listview and I cannot figure out the
syntax to override ListView.Items.Add().
"Properties" don't have methods.

I think you're confusing the ListView with its property Items in this case.
The method Add doesn't belong to ListView, it belongs to its instance of
ListViewItemCollection, which the property Items returns.

To override "ListView.Items.Add()" you actually would have to overide *two*
classes; One class that inherits the ListView class to "override" the Items
property (which is *not* virtual, so it can't be "truly" overridden), and a
class that inherits ListViewItemCollection to override Add, but...
I can do what I need to do in a different way
this would just make everything significantly
cleaner and eaasier to maintain.


....if you really *told* us what you "need" to do, we probably can help you
find another "clean and easy" way... ;-)

// Bjorn A
Nov 16 '05 #4

P: n/a
Sorry for not being clear with my needs I will elaborate after a quick
explanation of the path I have taken.

I am trying to do exactly as you stated: override the ListView Items
properties Add method. I think I have figured out a way to do it, well kind
of, since as you stated the Items property is not virtual and cannot be
overriden using the override keyword, I used the following syntax to
effectively override it or at least it appears to work as I need it to. It
returns my local__CustomView.Items collection. So now I will just need to
make sure the external Listview.Items collection is updated with the
CustomView.Items collection

new public System.Windows.Forms.ListView.ListViewItemCollecti on Items

{

get

{

//return the __CustomView.Items collection so its collection will be
updated; Don't update the base ListView.Items collection

// we will do that using events as needed in the UI

return __CustomView.Items;

}

}

O.K. now I will try to explain why I am trying to do this. Our app is using
2 listviews

1) to represent our embedded products main features

2) each possible instance of the main feature in the 1st ListView (normally
250 instances per main Feature)

We needed a way to (1)show all possible instances and (2)only the instances
that the user has defined. So effectively show all of the instances or only
show a subset of the possible instances. So I needed a Custom ListView that
I can add all the Items (to store colors and text for each possible
instance) then display the Full set or the subset in a different ListView in
the UI. Since adding Items or changing text(name) and enabled state is done
in various Classes throughout the solution I wanted a separate class that
holds the Items to help keep it "clean" or easy to maintain as we are
constantly updating our embedded products feature set. My next issue will be
to keep the indexes straight between the 2 views as clicking on an Item in
the ListView populates our main UI with that instances data. So in the Full
view the indexes are linear from min to max number of instances and in the
subset view they will be non linear as only defined instances will be
available in the ListView. So index 200 in Full view could be index 2 in
subset view.

I hope this adequately explains what I am trying to do.

Thanks,

Dave

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:eR**************@TK2MSFTNGP11.phx.gbl...

"Dave Y" wrote...
I am a newbie to C# and am having trouble
trying to override a ListView property method.
I have created a new class derived from the
Forms.Listview and I cannot figure out the
syntax to override ListView.Items.Add().
"Properties" don't have methods.

I think you're confusing the ListView with its property Items in this

case. The method Add doesn't belong to ListView, it belongs to its instance of
ListViewItemCollection, which the property Items returns.

To override "ListView.Items.Add()" you actually would have to overide *two* classes; One class that inherits the ListView class to "override" the Items property (which is *not* virtual, so it can't be "truly" overridden), and a class that inherits ListViewItemCollection to override Add, but...
I can do what I need to do in a different way
this would just make everything significantly
cleaner and eaasier to maintain.


...if you really *told* us what you "need" to do, we probably can help you
find another "clean and easy" way... ;-)

// Bjorn A

Nov 16 '05 #5

P: n/a
Sorry for not being clear with my needs I will elaborate after a quick
explanation of the path I have taken.

I am trying to do exactly as you stated: override the ListView Items
properties Add method. I think I have figured out a way to do it, well kind
of, since as you stated the Items property is not virtual and cannot be
overriden using the override keyword, I used the following syntax to
effectively override it or at least it appears to work as I need it to. It
returns my local__CustomView.Items collection. So now I will just need to
make sure the external Listview.Items collection is updated with the
CustomView.Items collection

new public System.Windows.Forms.ListView.ListViewItemCollecti on Items

{

get

{

//return the __CustomView.Items collection so its collection will be
updated; Don't update the base ListView.Items collection

// we will do that using events as needed in the UI

return __CustomView.Items;

}

}

O.K. now I will try to explain why I am trying to do this. Our app is using
2 listviews

1) to represent our embedded products main features

2) each possible instance of the main feature in the 1st ListView (normally
250 instances per main Feature)

We needed a way to (1)show all possible instances and (2)only the instances
that the user has defined. So effectively show all of the instances or only
show a subset of the possible instances. So I needed a Custom ListView that
I can add all the Items (to store colors and text for each possible
instance) then display the Full set or the subset in a different ListView in
the UI. Since adding Items or changing text(name) and enabled state is done
in various Classes throughout the solution I wanted a separate class that
holds the Items to help keep it "clean" or easy to maintain as we are
constantly updating our embedded products feature set. My next issue will be
to keep the indexes straight between the 2 views as clicking on an Item in
the ListView populates our main UI with that instances data. So in the Full
view the indexes are linear from min to max number of instances and in the
subset view they will be non linear as only defined instances will be
available in the ListView. So index 200 in Full view could be index 2 in
subset view.

I hope this adequately explains what I am trying to do.

Thanks,

Dave

"Jack Smash" <so*****@somewebsite.com> wrote in message
news:uo****************@tk2msftngp13.phx.gbl...
I'm sorry, that last question probably wasn't too helpful,
but if you're overriding a virtual method, just make sure you include the
"override" key word.
But it is important to note that the ListView.Items.Add() method is not in
the ListView class itself. I do not know the implementation for Items - but it is probably an ArrayList or something like that.
"Jack Smash" <so*****@somewebsite.com> wrote in message
news:Oj**************@TK2MSFTNGP11.phx.gbl...
Where is it exactly that you are having troubles with it?
"Dave Y" <me@work.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the

Forms.Listview
and I cannot figure out the syntax to override ListView.Items.Add(), .
I
see
that it is a virtual method so it should be easy to do. If anyone can

help
I
would appreciate it greatly. I can do what I need to do in a different

way this would just make everything significantly cleaner and eaasier to
maintain.



Nov 16 '05 #6

P: n/a

"Dave Y" wrote...
I think I have figured out a way to do it, well kind
of, since as you stated the Items property is not
virtual and cannot be overriden using the override
keyword, I used the following syntax to
effectively override it or at least it
appears to work as I need it to.
Using the "new" keyword don't truly override, but "hides"
it and should work as long as the variables referencing
to it is of the type of your derived class.
We needed a way to
(1) show all possible instances and
(2) only the instances that the user has defined.
I believe you're on the right track with the use of some "backing" list,
containing all of your items, but I'm not sure your approach is the most
"clean and easy" one.

I'm not sure that a "Custom ListView" is needed at all, but rather some
specialized collection containing ListViewItems (representing all of your
"instances"), from which you can "fetch" arrays of items to populate the
ordinary ListViewCollection with, depending on the users preferences.
My next issue will be to keep the indexes straight
between the 2 views as clicking on an Item in
the ListView populates our main UI with that
instances data.


IMHO, the indices shouldn't be used in that way
at all. You can instead use the Tag property
of a ListViewItem to contain a reference to the
"instance" of your product.

Just my 2c.

// Bjorn A
Nov 16 '05 #7

P: n/a
That's a good idea, I'll see if I can make use of the suggestion. Thanks for
your 2c. Liek I said I am new to this and am very wlling to learn the ways
of making my life easier. Some habbits die hard, as a c programmer I am just
comfortable with using indexes.

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...

"Dave Y" wrote...
I think I have figured out a way to do it, well kind
of, since as you stated the Items property is not
virtual and cannot be overriden using the override
keyword, I used the following syntax to
effectively override it or at least it
appears to work as I need it to.


Using the "new" keyword don't truly override, but "hides"
it and should work as long as the variables referencing
to it is of the type of your derived class.
We needed a way to
(1) show all possible instances and
(2) only the instances that the user has defined.


I believe you're on the right track with the use of some "backing" list,
containing all of your items, but I'm not sure your approach is the most
"clean and easy" one.

I'm not sure that a "Custom ListView" is needed at all, but rather some
specialized collection containing ListViewItems (representing all of your
"instances"), from which you can "fetch" arrays of items to populate the
ordinary ListViewCollection with, depending on the users preferences.
My next issue will be to keep the indexes straight
between the 2 views as clicking on an Item in
the ListView populates our main UI with that
instances data.


IMHO, the indices shouldn't be used in that way
at all. You can instead use the Tag property
of a ListViewItem to contain a reference to the
"instance" of your product.

Just my 2c.

// Bjorn A

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.