Results 1 to 12 of 12

Thread: [RESOLVED] Add Property to Listview items ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2023
    Posts
    105

    Resolved [RESOLVED] Add Property to Listview items ?

    With following Code (class) i add a Property to a ListView itself.
    How to add that property to all ListView items ?

    Code:
    Public Class MyListView
        Inherits ListView
    
        Private _Tag1 As Object
        Public Property Tag1() As Object
            Get
                Return _Tag1
            End Get
            Set(ByVal value As Object)
                _Tag1 = value
            End Set
        End Property
    
    End Class
    Many ways exist to Babylon.
    Only the shortest is the Best.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,742

    Re: Add Property to Listview items ?

    It'll be the same value for all of your items, that way
    Can you show your adding items code?

  3. #3

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2023
    Posts
    105

    Re: Add Property to Listview items ?

    Quote Originally Posted by .paul. View Post
    It'll be the same value for all of your items, that way
    Can you show your adding items code?
    I wont to store in Tag1 another information (like in the second line.
    Code:
    lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = "Pheasant*"
    
    lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag1 = "D:\Pics\image1""
    Many ways exist to Babylon.
    Only the shortest is the Best.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,742

    Re: Add Property to Listview items ?

    An Objects Tag Property is Type Object. You can put anything you choose in there...

    For example, a Bitmap. You need to cast the Tag to the appropriate Type when you retrieve it...

    Code:
    lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = New Bitmap("D:\Pics\image1.png")
    For simple strings, try this...

    Code:
    lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = New List(Of String)(New String() {"Pheasant*", "D:\Pics\image1"})
    Code:
    Private Sub lstAnimal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAnimal.SelectedIndexChanged
        MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, List(Of String))(0))
        MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, List(Of String))(1))
        'add another
        DirectCast(lstAnimal.FocusedItem.Tag, List(Of String)).Add("Third string")
        MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, List(Of String))(2))
    End Sub
    Last edited by .paul.; Oct 14th, 2024 at 02:12 PM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2023
    Posts
    105

    Re: Add Property to Listview items ?

    Thanks for the Code , i will try this out.(this is another way)

    You mean the Add Property its not Possible for ListView items ?
    Many ways exist to Babylon.
    Only the shortest is the Best.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,742

    Re: Add Property to Listview items ?

    Quote Originally Posted by marsias View Post
    ...You mean the Add Property its not Possible for ListView items ?
    It's possible, but not really necessary, as the tag can hold anything you need it to...

    Code:
    Public Class TagExtenderClass
    
        Public Tag1 As String
        Public Tag2 As String
        Public Tag3 As String
    
    End Class
    Code:
    lstAnimal.Items.Add("Ring-Necked Pheasant", 0).Tag = New TagExtenderClass With {.Tag1="First String", .Tag2="Second String" , .Tag3="Third String"}
    Code:
    Private Sub lstAnimal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAnimal.SelectedIndexChanged
        MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, TagExtenderClass ).Tag1)
        MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, TagExtenderClass ).Tag2)
        MsgBox(DirectCast(lstAnimal.FocusedItem.Tag, TagExtenderClass ).Tag3)
    End Sub

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,742

    Re: Add Property to Listview items ?

    Check the link in post #3 if you want to add properties

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,677

    Re: Add Property to Listview items ?

    The problem with the code in post #1 is that you extended the ListView ... not ListViewItem ...
    Post 4 uses the default .Add method which would use the default ListViewItem ... not your custom one. You'd have to inherit ListView and create a .Add2 that returns your custom ListViewItem with the Tag2 property, then you'd be able to use it. But then you'd have to replace all your list view with your custom one .... you can see how this is getting out of hand quickly.
    All that said... the solution .Paul posted in post #7 is likely the best solution. The .Tag property is just an Object type, so you can put anything you want into it, so why not make that your complex object? That's far more elegant than trying to inherit and override the base controls and so on...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2023
    Posts
    105

    Re: Add Property to Listview items ?

    I see yet that the Pauls solution is a simply one and working Solution (already applied).
    I always search for a simply Solution (1-2 lines of Code) and not to write a bunches of Code to resolve the same Problem(like in external link from Paul).
    As i see, these steps can also applied to all Controls which have a Collection of items(Combobox, listBox, etc)
    Can this steps also applied to other Properties like Forecolor to have a disabled Forecolor?
    Many thanks again to Paul
    Many ways exist to Babylon.
    Only the shortest is the Best.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,742

    Re: Add Property to Listview items ?

    Unfortunately, you're not quite right about ComboBox and ListBox items. For those, you'd use a slightly different method...

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,742

    Re: Add Property to Listview items ?

    Quote Originally Posted by marsias View Post
    ...Can this steps also applied to other Properties like Forecolor to have a disabled Forecolor?
    I'm not sure what you mean there. There are ways to stop the forecolor being editable, but that's not related to the topic of this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width