vb.netcomponentone

Component1: How to Add DictionaryEntry object to C1List as item?


I have to replace .net Listbox with Component one C1List from existing application. Previously it items were added like below.

 lstFrmType.Items.Add(New DictionaryEntry("False", 
                                   FormatPercent(-0.1234, 
                                   CInt(numFrmDecimalPlaces.Value), 
                                   TriState.True, 
                                   TriState.False, 
                                   TriState.False)))

But for component one C1list i can see that it have new mehtod AddItem() but it only accept the parameter as string. I cannot add DictionaryEntry object.

 lstFrmType.AddItem(New DictionaryEntry("False", 
                                   FormatPercent(-0.1234, 
                                   CInt(numFrmDecimalPlaces.Value), 
                                   TriState.True, 
                                   TriState.False, 
                                   TriState.False)))

Is there any other way to achieve this?


Solution

  • There are certain limitations when you use C1List in unbound mode (AddItem is possible only in unbound mode). In an unbound mode you cannot use DisplayMember/ValueMember which you would definitely need here to use your DictionaryEntry object. Therefore it is better to use bound mode (DataMode = Normal). You can write an extension that looks as if we are using AddItem but behind the scene you can push data into list's DataSource.

    Imports System.Runtime.CompilerServices
    Imports C1.Win.C1List
    Imports System.ComponentModel
    
    Module C1ListExtensions
        <Extension()>
        Public Sub AddItem(ByVal list As C1List, ByVal item As DictionaryEntry)
            If list.DataMode = DataModeEnum.AddItem Then
                Throw New Exception("Need DataMode to be DataMode.Normal")
            Else
                If list.DataSource Is GetType(BindingList(Of Tuple(Of Object, Object))) Then
                    ' Set DisplayMember and ValueMember to Item1 and Item2
                    DirectCast(list.DataSource, BindingList(Of Tuple(Of Object, Object))).Add(New Tuple(Of Object, Object)(item.Key, item.Value))
                Else
                    Throw New Exception("Implement your own DataSource here")
                End If
            End If
        End Sub
    End Module
    

    The only limitation with this method is that you have to implement this extension as per you DataSource type.