|
-
February 2nd, 2009, 06:47 PM
#1
[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
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)]
-
February 2nd, 2009, 10:26 PM
#2
Re: how to set the text property on a checkedlistbox collection
-
February 3rd, 2009, 07:24 AM
#3
Re: how to set the text property on a checkedlistbox collection
 Originally Posted by dglienna
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)]
-
February 3rd, 2009, 08:12 AM
#4
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)]
-
February 3rd, 2009, 08:50 PM
#5
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
-
February 4th, 2009, 06:59 AM
#6
Re: how to set the text property on a checkedlistbox collection
 Originally Posted by dglienna
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)]
-
February 4th, 2009, 07:57 PM
#7
Re: how to set the text property on a checkedlistbox collection
You need to add KEY and VALUE combos to a DICTIONARY
-
February 5th, 2009, 03:17 AM
#8
Re: Problem setting the default text property on a checkedlistbox collection
 Originally Posted by Marraco
(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]
-
February 5th, 2009, 09:27 AM
#9
Re: how to set the text property on a checkedlistbox collection
 Originally Posted by dglienna
You need to add KEY and VALUE combos to a DICTIONARY
In this case Values are dictionaries each one.
[Vb.NET 2008 (ex Express)]
-
February 5th, 2009, 09:33 AM
#10
Re: Problem setting the default text property on a checkedlistbox collection
 Originally Posted by jmedved
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)]
-
March 11th, 2009, 09:51 AM
#11
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!
-
March 12th, 2009, 02:54 PM
#12
Re: how to set the text property on a checkedlistbox collection
 Originally Posted by WRS_Darren
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|