Results 1 to 3 of 3

Thread: [RESOLVED] Custom objects to populate Listview Items collection

  1. #1

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    [RESOLVED] Custom objects to populate Listview Items collection

    Hi

    I have created a class by inheriting the ListViewItem and then added some new properties. I then use this inherited class to define objects that I add to my listview. My question is, how do I pick up the new properties when I click on an item.
    VB Code:
    1. Class CMyItem
    2.    inherits ListViewItem
    3.  
    4.    Public ComponentName as string
    5.    Public AccessID as Long
    6.    Public Sub New(byval agStrCompName as string, byval agLngAccessID as Long)
    7.  
    8.       ComponentName = agStrCompName
    9.       AccessID = agLngAccessID
    10.    End Sub
    11. End Class
    12.  
    13. ' In a form I add items to my listview as follows
    14.   Dim objItem as CMyItem
    15.  
    16.   objItem = new CMyItem("ComponentA", 16)
    17.  
    18.   ListView1.Add(objItem)
    19.  
    20.  
    21.   Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles ListView1.Click
    22.         ' How do I pick up the Componentname property from my custome made item that populates the listview
    23.     End Sub

    Now I could be using an incorrect approach, I would welcome any alernatives. Just bear in mind that I want to populate my listview with custom made objects that would expose my new properties.

    Thanks for your help.
    Last edited by Mr.No; Dec 17th, 2002 at 06:01 AM.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    I took the liberty to add 2 properties as well.

    Code:
    Class CMyItem
        Inherits ListViewItem
    
        Dim m_ComponentName As String
        Dim m_AccessID As Long
    
        Public Sub New(ByVal agStrCompName As String, _
            ByVal agLngAccessID As Long)
            ComponentName = agStrCompName
            AccessID = agLngAccessID
        End Sub
    
        Property ComponentName()
            Get
                Return m_ComponentName
            End Get
            Set(ByVal Value)
                m_ComponentName = Value
            End Set
        End Property
    
        Property AccessID()
            Get
                Return m_AccessID
            End Get
            Set(ByVal Value)
                m_AccessID = Value
            End Set
        End Property
    
    End Class
    to get something back you can do something like this.....

    Code:
    MsgBox(DirectCast(ListView1.SelectedItems.Item(0), CMyItem).AccessID)

  3. #3

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Thumbs up [RESOLVED] Custom objects to populate Listview items collection

    Thanks Athley.

    It works
    Last edited by Mr.No; Dec 17th, 2002 at 05:51 AM.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

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