ComboBox with datasource = ArrayList property of an object not displaying newly added item's text.

Posted on 2006-05-14
Last Modified: 2007-12-19
I've got a class "Customer" with a public property called "Hosts", which is an ArrayList of a class called "Host" which has 2 properties ("HostID" and "Name").

In Form_Load, after populating the objects Customer and Hosts from a database, the combobox propertly shows the list of Hosts by the foloowing code
Ptivate Sub LoadHostCombo()
        Me.comboHost.DataSource = Nothing
        Me.comboHost.DataSource = AppSession.CurrentCustomer.Hosts
        Me.comboHost.DisplayMember = "Name"
        Me.comboHost.ValueMember = "HostID"
End Sub

I then set the SelectedIndex to 0 and all is groovy (i.e. the comboBox displays the propery text, my SelectedIndexChanged event fires (it populates the form with the selected Hosts's data).

I have a LinkLabel captioned New"that prompts the user to enter a new Host Name, adds it to the Hosts collection of CurrentCustomer, and then calls LoadHostCombo to refresh the DataSource for the combobox.  I then attempt to set the SelectedIndex property of the combobox to select the newly added item, which in turn fires the SelectedIndexChanged event that displays the Hosts's data on the form.

Everything works fine except that the text in the combobox doesn't change, i.e. I see the new Hosts data on the form where expected, but the text in the combo displays the originally selected Host's Name.  From that point on, even if I manually select another preexisting Host in the combo, I see the proper data on my form, but the combobox refuses to display the selected items text.

What am I missing?  Oh yeah, it get's totally screwy if i break in debug, because when i come back to the form, the combobox sometimes is just white-space (no dropdown arrow or nothing), and sometimes it comes back with a combobox sized piece of the debug window inplace of the control.  I can click on the right side and it functions like a combo box, but it's got busshit in the visuals...


please help

Question by:digibaba
    LVL 44

    Accepted Solution

    Hi digibaba,

    if understand your setup the Arraylist needs to send a refresh to the combo, this can done 2 ways implementing the IBindingList Interface or more simple rebinding the arralist to the combo but then you'll need to preserve the selected item and set the selected index after the rebind

    hope this helps a bit

    Author Comment

    Thanks bruintje,

    I was actually resetting the DataSource but I was getting the weird results as described above.  The IBindLinst is cool and although I probably won't use it this time around, I'm glad to know it it and therefore I'll accept your answer. This, despite the fact it didn't solve my problem.  

    I solved it myself from reslizing that (or maybe windows Forms) seems to have dragged along a NASTY bug in ComboBox from VB6....  In fact it's nastier now because it doesn't throw an error, but just gets all screwy instead.  i.e.  you cannot create/remove controls from within a combobox event or any subprocudure called from that event.  VB6 used to give you a stupid error, and .net just acts screwy.  

    The "solution" (hack)  was to put a timer on the form, enable it on the combobox event, have it fire up the procudre that cretes/removes controls and disable itself.  What a hack.  Whay did they do this???

    You know how the IDE runtime seems to "hang" for a 2-3 seconds before it throws an error?  Well in this case it was hanging like that, but never threw an error.  It just totallt messes up the combobox instead.   Grrrrrr.

    Thanks for the tip.



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now