• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1444
  • Last Modified:

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

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

1 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
source: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/7a6517df117b3215

hope this helps a bit
digibabaAuthor Commented:
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 vb.net (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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now