Question Overriding ListView.Items

kimosavi

Active member
Joined
Apr 9, 2009
Messages
34
Location
Chicago, IL
Programming Experience
3-5
Hi,

I want to be able to create my own list of of listviewitems by adding my own properties and variables. I tried the Override but it doesn' work, only the Overloads or Shadows seems to be working.

so far i have done this:

Public Class modListView
Inherits ListView
Private modList As ModListViewItemCollection
Public Overloads ReadOnly Property Items() As ModListViewItemCollection
Get
Return modList
End Get
End Property
End Class

Public Class ModListViewItemCollection
Inherits modListView.ListViewItemCollection
Sub New()
MyBase.New(New modListView)
End Sub
End Class

Public Class ModListViewItem
Inherits ListViewItem
Public Var1 As String
Public Var2 As String
Public Var3 As String
Public Property1 as String
Public Property2 as String
Public Property3 as String
End Class

I was able to add these controls to the GUI. my ModListView is using a ImageList called myImages and contains 4 images. I did the connection in the designer window.

I my new ModListView I can go to the properties and I can see the ITEMS property now classified under MISC. when I click [...] I can add ViewListItems but for some reason when I click on the ImageIndex Property I there is no items there to select the image.

What am I doing wrong?

I also tried to following without success:

Public Class modListView
Inherits ListView
Private modList As New List(Of ModListViewItems)
Public Shadows ReadOnly Property Items() As List(Of ModListViewItems)
Get
Return modList
End Get
End Property
End Class

Public Class ModListViewItem
Inherits ListViewItem
Public Var1 As String
Public Var2 As String
Public Var3 As String
Public Property1 as String
Public Property2 as String
Public Property3 as String
End Class
 
If you're only going to add items in code then can just use a standard ListView. A ModListViewItem is a ListViewItem, courtesy of inheritance, so you can add ModListViewItem objects to the Items collection of a standard ListView. You'll have to cast each item as you get it from the Items collection but that's not too much of an ordeal.

If you want to be able to add your custom items at run time then you will have to either add a property with a different name or else shadow the existing Items property. You can't override a member that isn't declared Overridable, which Items isn't because it isn't intended to be replaced. Not only that, you'll have to provide your own designer too, which is not a trivial exercise. Because you're shadowing, there's no way to stop someone casting as type ListView and accessing the base member anyway. Also, you'd have to make sure that, internally, any items added to your custom collection were also added to the standard collection.
 
thanks jmcilhinney for your reply.

I tried the following and works like a charm:

for each myListItem as ModListViewItem in ModListView.Items
msgbox(myListItem.Variable1)
msgbox(myListItem.Variable2)
next

I went down that path of trying to control the items the way wanted to see how deep the rabbit whole was :)

I wanted to make sure I could control my items as desired, add global variables, sub, functions and properties to them, while maintaining the graphical properties and being able to change the information on design and run-time.

I tried doing it through the Shadows declaration but for some reason while in design mode I cannot change the listview.imageindex. The imagelist seems to be detached from my modlistviewitem class and don't know how to assign it.

design mode

properties
modviewlist.imagelist = myimagelist
modviewlist.items [(collection][...]
when i hit this it goes to the dialog to add the modviewlistitems.

in this dialog i click [add] and when I do i scroll down to the properties to change the imageindex but it is empty, no values. Eventhough modviewlist.imagelist was assign.

do you know why i don't have a list of images showing for the imageindex of the modviewlist properties?
 
Back
Top