Click to See Complete Forum and Search --> : [RESOLVED] how to set the text property on a checkedlistbox collection


Marraco
February 2nd, 2009, 05:47 PM
I had read on pages like this (http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=38572)

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

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
(Collection)
How is the right way?

(the overriden function ToString() as is never called)

dglienna
February 2nd, 2009, 09:26 PM
Nope. Read this:

http://www.c-sharpcorner.com/UploadFile/mgold/SortedDictionarySample06202007142815PM/SortedDictionarySample.aspx

Marraco
February 3rd, 2009, 06:24 AM
Nope. Read this:

http://www.c-sharpcorner.com/UploadFile/mgold/SortedDictionarySample06202007142815PM/SortedDictionarySample.aspxI 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.

Marraco
February 3rd, 2009, 07:12 AM
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
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() )

dglienna
February 3rd, 2009, 07:50 PM
Why can't you override it?

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

Marraco
February 4th, 2009, 05:59 AM
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.
...
Public Class Employee
Inherits SortedDictionary

...
Public Overrides Function ToString() As String
Return FirstName & " " & LastName
End Function
End Class

dglienna
February 4th, 2009, 06:57 PM
You need to add KEY and VALUE combos to a DICTIONARY

jmedved
February 5th, 2009, 02:17 AM
(the overriden function ToString() as is never called)

There is always possibility of making "envelope" class like
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
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:System.Collections.Generic.SortedDictionary'2[System.String,something]

Marraco
February 5th, 2009, 08:27 AM
You need to add KEY and VALUE combos to a DICTIONARY
In this case Values are dictionaries each one.

Marraco
February 5th, 2009, 08:33 AM
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.

WRS_Darren
March 11th, 2009, 09:51 AM
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!

Marraco
March 12th, 2009, 02:54 PM
Just spent hours...
Hope that helps!It certainly helps!
Thanks Darren!:D