Bind a listbox to a dictionary object

J

john wright

I have a dictionary oject I created and I want to bind a listbox to it. I
am including the code for the dictionary object.

Here is the error I am getting:

"System.Exception: Complex DataBinding accepts as a data source either an
IList or an IListSource

at System.Windows.Forms.ListControl.set_DataSource(Object value)

at USBSecure.frmUSBSecure.ExchangeInputAndOutputReports() in
d:\usbsecure\USBSecure\frmUSBSecure.vb:line 743"


I tried to implement the IList interface and got the following:
'USBSecure.FOBS' must implement 'Overridable Function IndexOf(value As
Object) As Integer' for interface 'System.Collections.IList'.

So I try to override it and get an error that I can't do this because it is
not overriding a sub in the base class. All I want is to bind my listbox to
my dictionary. Any help is appreciated.

John


FOB Dictionary CODE:
Imports System.Collections

Public Class FOB

Public Enum FOBStatus

Enabled = 1

Disabled = 2

Validated = 3

Invalid = 4

End Enum



Private strFOBID As String

Private intFOBStatus As Integer

#Region "Constructors"

Public Sub New(ByVal strFOBNumber As String, ByVal intFOBStat As Integer)

strFOBID = strFOBNumber

intFOBStatus = intFOBStat

End Sub

Public Sub New()

End Sub

#End Region

#Region "Properties"

Public Property FOBID() As String

Get

Return strFOBID

End Get

Set(ByVal Value As String)

strFOBID = Value

End Set

End Property

Public Property Status() As Integer

Get

Return intFOBStatus

End Get

Set(ByVal Value As Integer)

intFOBStatus = Value

End Set

End Property

#End Region

End Class

Public Class FOBS

Inherits DictionaryBase

#Region "Public Methods"

Public Sub add(ByVal KeyFOB As FOB)

MyBase.Dictionary.Add(KeyFOB.FOBID, KeyFOB)

End Sub

Public Sub add(ByVal KeyFOB As String, ByVal FOBStatus As Integer)

MyBase.Dictionary.Add(KeyFOB, New FOB(KeyFOB, FOBStatus))

End Sub

Public Sub Remove(ByVal FOBID As String)

MyBase.Dictionary.Remove(FOBID)

End Sub

Public Sub Remove(ByVal KeyFOB As FOB)

MyBase.Dictionary.Remove(KeyFOB.FOBID)

End Sub

Public Function Contains(ByVal Keyfob As FOB) As Boolean

Return MyBase.Dictionary.Contains(Keyfob.FOBID)

End Function

Public Function Contains(ByVal KeyFOB As String) As Boolean

Return MyBase.Dictionary.Contains(KeyFOB)

End Function

#End Region

#Region "Properties"

Default Public Property FOB(ByVal KeyFOB As String) As FOB

Get

Return MyBase.Dictionary.Item(KeyFOB)

End Get

Set(ByVal Value As FOB)

MyBase.Dictionary.Item(KeyFOB) = Value

End Set

End Property

#End Region

Protected Overrides Sub finalize()

MyBase.Finalize()

End Sub

End Class
 
K

Ken Tucker [MVP]

Hi,

Here is a custom dictonary I use for binding to. I added a
bindablelist property which returns an arraylist of the values. You can
bind to the bindablelist.

Public Class BindableDictonary
Inherits DictionaryBase


Default Public Property Item(ByVal key As Object) As Object
Get
Return Dictionary(key)
End Get
Set(ByVal Value As Object)
Dictionary(key) = Value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(ByVal key As Object, ByVal value As Object)
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(ByVal key As Object) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(ByVal key As Object)
Dictionary.Remove(key)
End Sub 'Remove


Public ReadOnly Property BindableList() As ArrayList
Get
Dim al As New ArrayList
For Each de As DictionaryEntry In Me.Dictionary
al.Add(de.Value)
Next
Return al
End Get
End Property
End Class


Simple example

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim bd As New BindableDictonary

For x As Int16 = 10 To 0 Step -1
Dim cls As New Bindto
With cls
.ColA = String.Format("Col A {0}", x)
.ColB = String.Format("B {0}", x)
End With
bd.Add(x, cls)
Next

DataGrid1.DataSource = bd.BindableList
End Sub
End Class


Public Class Bindto
Dim mstrColA As String
Dim mstrColB As String

Public Property ColA() As String
Get
Return mstrColA
End Get
Set(ByVal Value As String)
mstrColA = Value
End Set
End Property

Public Property ColB() As String
Get
Return mstrColB
End Get
Set(ByVal Value As String)
mstrColB = Value
End Set
End Property
End Class

Ken
-------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top