By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Bind a listbox to a dictionary object

P: n/a
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(Ob ject value)

at USBSecure.frmUSBSecure.ExchangeInputAndOutputRepor ts() 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


Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
-------------------
"john wright" <ri**********@hotmail.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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(Ob ject value)

at USBSecure.frmUSBSecure.ExchangeInputAndOutputRepor ts() 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

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.