473,287 Members | 1,709 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,287 software developers and data experts.

Override Listview property method

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
7 6397
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
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

"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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Richard | last post by:
Hi. I use a ListView to display data in tabular form. Each ListView row corresponds to a data record. The ListView Item of the record is the record key or code. Each SubItem in that row...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
2
by: Aaron Queenan | last post by:
Is there any way to know whether the OnItemCheck is being called in response to a user action (mouse or keyboard) as opposed to the form loading? I have a class which derives from...
5
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window {...
13
by: Maheshkumar.R | last post by:
hi groups, I have placed an listview control, i want to iterate thru the control and find the clicked event items. listView2.Items.Add(fname.ToString(), i); how i can perform the iteration...
6
by: Nick | last post by:
Hi there, I'm trying to implement drag-drop for my listview control in large icon view mode. Unfortunately the order of the items gets completely messed up upon inserting the item back into the...
6
by: Dave | last post by:
VB6 has a SorkKey property that you can setup on the ListView control to tell the ListView what column to use for sorting. In .NET there is a Sort() method and a SortOrder property that you can...
4
by: Rob | last post by:
I've constructed a user control inherited from ListView so I can handle and respond to scrolling events (to keep 2 listviews scrolling in sync). My user control includes an Overrides of WndProc...
12
by: garyusenet | last post by:
I have had no replies to my previous post so perhaps I didn't write it good enough. Please excuse new thread but i wanted to break from the last thread hopefully this thread will be better. ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.