Results 1 to 3 of 3

Thread: [2005] Objects ListView

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    [2005] Objects ListView

    All,

    I have just had to change my ListBox to a ListView. Now I need to keep an Object (Custom Class) in the ListView, this was fine when it was a list box as I just overrode the tostring method. How can I store the object in a listviews sub item or somewhere relative to the data row.

    Thanks

    Code is below I just need to add the object in as well

    Code:
        Private Function populateListView(ByVal emailContact As ContactDetails) As Windows.Forms.ListViewItem
            Dim tmp As String = emailContact.AccountRef
            Dim itmp As New Windows.Forms.ListViewItem(tmp)
    
            Dim itmp2 As New Windows.Forms.ListViewItem.ListViewSubItem(itmp, CStr(emailContact.checkDataLevel))
            Dim itmp3 As New Windows.Forms.ListViewItem.ListViewSubItem(itmp, emailContact.EmailAddress)
    
            itmp.SubItems.Add(itmp2)
            itmp.SubItems.Add(itmp3)
    
            Return itmp
        End Function

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Objects ListView

    I think that you must do one of the following:

    1. Create a new class that inherits the ListViewItem and create a new property for holding this object. Then use this instead of the ordinary ListViewItem.

    2. Put the object in the ListViewItems Tag property.

    Personally I'd go for the first option.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Objects ListView

    If you opt for Atheists first suggestion then you could also overload the constructor, pass an instance of your class and add the appropriate subitems right there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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