Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Collection, IList and Listbox... updating

Status
Not open for further replies.

bppj

Programmer
Jun 6, 2002
24
US
I was investigating using custom business objects to represent my database data as opposed to datasets for my presentation level. Decided to try the typed custom collection thing. Anyway, here's my base test object:

Code:
Public Class TestObject
    Private mID As Integer
    Private mDesc As String

    Public Sub New(ByVal id As Integer, ByVal desc As String)
        mID = id
        mDesc = desc
    End Sub

    Public ReadOnly Property ID() As Integer
        Get
            Return mID
        End Get
    End Property

    Public ReadOnly Property Description() As String
        Get
            Return mDesc
        End Get
    End Property
End Class

Here is my typed collection:

Code:
Public Class TestCollection
    Inherits CollectionBase

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Add(ByVal testObj As TestObject)
        MyBase.List.Add(testObj)
    End Sub

    Public Sub Remove(ByVal testObj As TestObject)
        MyBase.List.Remove(testObj)
    End Sub
End Class[\code]

Anyway, so I whipped up a test form:

[code]Public Class testform
    Inherits System.Windows.Forms.Form

    Private col As New TestCollection()
    Private Shared count As Integer = -1

 ... 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim testObj As New TestObject(count, TextBox1.Text)
        col.Add(testObj)
        initbox()
        count -= 1
    End Sub

    Private Sub initbox()
        ListBox1.DataSource = Nothing
        ListBox1.DisplayMember = "Description"
        ListBox1.DataSource = col
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        col.Remove(CType(ListBox1.SelectedItem, TestObject))
        initbox()
    End Sub

End Class

... idea being, try developing a object add/delete control. The text box was used to enter new description, button1 is to add, button2 to remove the selected object in the listbox.

Everything works great except when I remove the last item in the listbox... then, instead of displaying my "Description", the listbox displays the default "ToString" and the listbox has no selected item. When I go to select an item in the listbox, I get :

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Specified argument was out of the range of valid values.


Anyway, I'm obviously missing something basic here. Until I understand, I really don't want to pursue this path.

Any help in explaining this behaviour would be GREATLY appreciated.

Thanks,

B.J.
 
Would still love any insight into this behavior, but I did find a workaround...

In looking into the scenario, it seems to have a truely fully functional Collection for binding, you need not only to implement IList, but IBindingList. That is, to have the collection dynamically update the controls it is bound to when the data changed. Info here:


Anyway, there are two workarounds suggested, one of which I had implemented: setting control datasource to nothing and then back to arraylist(collection). That's when I get the behavior above. I implemented the other workaround, getting and refreshing the controls currencymanager, in my new sub below:

Code:
 Private Sub Initbox()
        Dim bm As BindingManagerBase = ListBox1.BindingContext(col)
        Dim cm As CurrencyManager = CType(bm, CurrencyManager)
        If Not (cm Is Nothing) Then cm.Refresh()
        ListBox1.DisplayMember = "Description"
End Sub

And I seem to have gotten rid of my "bug".

I would still love any further insight into this issue, but I figured I could maybe help someone else with what I did find...

B.J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top