CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    [RESOLVED] how to set the text property on a checkedlistbox collection

    I had read on pages like this

    that when adding objects to a CheckedListBox, it takes the .ToString() function of each added object as the text string.

    I added instances of SortedDictionary as this

    Code:
    Friend Class MySortedDictionary
        inherits SortedDictionary(Of string, something)
    
        Public Overrides Function ToString() as string
            return "I want to show this"
        end Function
    end Class
    ...
    CheckedListBox1.Items.Add(new MySortedDictionary)
    ...
    So, lets say, I want the text shown near each checkbox to be "I want to show this"
    But instead, I are seeing
    Code:
    (Collection)
    How is the right way?

    (the overriden function ToString() as is never called)
    Last edited by Marraco; February 2nd, 2009 at 07:19 PM. Reason: fixed bugs/misspellings
    [Vb.NET 2008 (ex Express)]

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to set the text property on a checkedlistbox collection

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: how to set the text property on a checkedlistbox collection

    Quote Originally Posted by dglienna View Post
    I don't get it.

    I don't want to add the sorted dictionary as the Item Collection of CheckedListBox1.

    I want to add objects. One by one.

    Since casually those object are SortedDictionary, the .ToString() function does not works, even when I override that function.
    Last edited by Marraco; February 3rd, 2009 at 06:31 PM.
    [Vb.NET 2008 (ex Express)]

  4. #4
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Problem setting the default text property on a checkedlistbox collection

    Up to date, I used imported VB6 checked list box (from automatically updated VB6 code). They used a VB6 data structure, but now I want to leave all that legacy code, and going totally .NET way.

    But I don't found how CheckedListBox is supossed to manage the shown string.
    since two days ago, I are revolving the MS documentation, and found little.

    In google I found things like
    Code:
    Me.CheckedListBox2.GetItemText
    that property, simply don't exist. Also InteliSense don't show a SetItemText method, so I supposed that the object added should incorporate the String to be displayed.

    then I found that It displays the .ToString function, but it looks like a clumsy solution, since many objects (like sortedDictionaries), can't be overridden ToString (it compiles, but does no effect as CheckedListBox matters, as if it acces the base class property instead of the overrided ToString() )
    [Vb.NET 2008 (ex Express)]

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to set the text property on a checkedlistbox collection

    Why can't you override it?

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            lstEmployees.Items.Add(New Employee("A", "B", DateTime.Now))
            lstEmployees.Items.Add(New Employee("C", "D", DateTime.Now))
            lstEmployees.Items.Add(New Employee("E", "F", DateTime.Now))
    
        End Sub
    End Class
    
    Public Class Employee
        Public FirstName As String
        Public LastName As String
        Public BirthDate As Date
    
        Public Sub New()
        End Sub
    
        Public Sub New(ByVal firstName, ByVal lastName, ByVal birthDate)
            Me.FirstName = firstName
            Me.LastName = lastName
            Me.BirthDate = birthDate
        End Sub
    
        Public Overrides Function ToString() As String
            Return FirstName & " " & LastName
        End Function
    End Class
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: how to set the text property on a checkedlistbox collection

    Quote Originally Posted by dglienna View Post
    Why can't you override it?
    When employee Inherits from SortedDictionary, overriding Function ToString(), compiles without problem, but the CheckedListBox control uses the base class SortedDictionary.ToString instead of the Employee.ToString overrided one.
    Code:
      ...
    Public Class Employee
        Inherits SortedDictionary
    
      ...
        Public Overrides Function ToString() As String
            Return FirstName & " " & LastName
        End Function
    End Class
    Last edited by Marraco; February 4th, 2009 at 07:11 AM.
    [Vb.NET 2008 (ex Express)]

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to set the text property on a checkedlistbox collection

    You need to add KEY and VALUE combos to a DICTIONARY
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: Problem setting the default text property on a checkedlistbox collection

    Quote Originally Posted by Marraco View Post
    (the overriden function ToString() as is never called)
    There is always possibility of making "envelope" class like
    Code:
    Friend Class XXX
    
        Public Sub New()
            Me._dictionary = New MySortedDictionary()
        End Sub
    
        Private _dictionary As MySortedDictionary
        Public ReadOnly Property Dictionary() As MySortedDictionary
            Get
                Return Me._dictionary
            End Get
        End Property
    
        Public Overrides Function ToString() As String
            Return "I want to show this"
        End Function
    
    End Class
    and then adding it to list with
    Code:
            CheckedListBox1.Items.Add(New XXX())

    P.S. Using ToString usually works perfectly with this. I think that this behavior has something to do with inner working of CheckedListBox since if MyBase.ToString() was used, you would see something like:
    Code:
    System.Collections.Generic.SortedDictionary'2[System.String,something]

  9. #9
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: how to set the text property on a checkedlistbox collection

    Quote Originally Posted by dglienna View Post
    You need to add KEY and VALUE combos to a DICTIONARY
    In this case Values are dictionaries each one.
    [Vb.NET 2008 (ex Express)]

  10. #10
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Problem setting the default text property on a checkedlistbox collection

    Quote Originally Posted by jmedved View Post
    There is always possibility of making "envelope" class like
    ...
    I did that, although is weird from Microsoft to use a property which may not work on undocumented cases. It should implement a specific interface with support for key, checkedstate and on the fly changes.

    It looks like it is a step back from VB6 version. So I searched for a .NET solution, but it looks like it don't exist.
    [Vb.NET 2008 (ex Express)]

  11. #11
    Join Date
    Mar 2009
    Posts
    1

    Talking Re: how to set the text property on a checkedlistbox collection

    Just spent hours working this out myself, thought I'd join and post as fairly recent thread...

    You shouldn't override the tostring method - you just need to use the DisplayMember property of checklistbox (N.B It does NOT come up in intellisense!).

    For example create a class like this:

    Private Class CheckedListItem
    Private myId As Long
    Private myTextToDisplay As String

    Public Sub New(ByVal Id As Long, ByVal TextToDisplay As String)
    myId = Id
    myTextToDisplay = TextToDisplay
    End Sub

    Public ReadOnly Property Id()
    Get
    Return myId
    End Get
    End Property

    Public ReadOnly Property TextToDisplay()
    Get
    Return myTextToDisplay
    End Get
    End Property

    End Class


    ...Then make sure you set the 'DisplayMember' as shown in following....

    Private Sub loadUsersList()
    Dim oUser As User
    Dim oListItem As CheckedListItem

    myUsersCol.loadAllUsers()

    ' Does not show up in intellisense!...
    Me.ckLstUsers.DisplayMember = "TextToDisplay"

    Me.ckLstUsers.Items.Clear()

    oListItem = New CheckedListItem(0, "<All Users>")
    Me.ckLstUsers.Items.Add(oListItem, True)

    For Each oUser In myUsersCol
    oListItem = New CheckedListItem(oUser.Id, oUser.UserFullName)
    Me.ckLstUsers.Items.Add(oListItem, True)
    Next
    End Sub

    (For ref: The displaymember property is inherited from ListControl so will be use-able with un-checked listboxes as well).

    Hope that helps!

  12. #12
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: how to set the text property on a checkedlistbox collection

    Quote Originally Posted by WRS_Darren View Post
    Just spent hours...
    Hope that helps!
    It certainly helps!
    Thanks Darren!
    [Vb.NET 2008 (ex Express)]

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

Featured