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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox.Text not working properly 1

Status
Not open for further replies.

RexHeadlong

Programmer
Apr 10, 2002
35
US
Does anybody have an idea why, when I assign a ComboBox.Text property, it does not appear to take?

I have a ComboBox with DropDownStyle = DropDown.
During the Load event handler of my form, I bind it to an ArrayList of strings.
But, I want the initial text in the ComboBox to be a string that is not in the ArrayList. So, I assign ComboBox.Text. But, I never see this text in the ComboBox and instead I see the text of the first string in the ArrayList.

Here is the code inside my form's Load handler:

Code:
this.cmBoxFormula.BeginUpdate();
ArrayList itemArray = new ArrayList(3);
itemArray.Add("item 1");
itemArray.Add("item 2");
itemArray.Add("item 3");
this.cmBoxFormula.DataSource = itemArray;
this.cmBoxFormula.SelectedIndex = -1;
this.cmBoxFormula.Text = "no item";
this.cmBoxFormula.EndUpdate();

In the debugger, I look at the properties just before the load event ends, and the .Text property is "no item", and the .SelectedIndex property is -1. Great. But, when the form appears on screen, what I see is "item 1" in the text area as if it were the selected item.

I put breakpoints in TextChanged and SelectedIndexChanged event handlers to see if there is some outside force changing the text or the selected item, but these do not get fired after my load event.

Can anybody shed some light on this? Thanks in advance for any help!
 
I remember this being a confirmed bug but I can't find the MS article.

Anyway, what I had to do was create an event handler for BindingContextChanged for the ComboBox and then set the Text property there. This event should fire the first time that your form is displayed. You may need to set the SelectedIndex to -1 twice for this to take.
 
If you are referring to MSDN article 327555, then I think that is what's happening. But, their suggested workaround
(setting .SelectedIndex = -1 before setting .Text) isn't working for me.

I'll give your suggestion a try. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top