ComboBox.Items.Clear() causes the selection of Text inside the edit of the ComboBox

ComboBox.Items.Clear() causes the selection of Text inside the edit of the ComboBox

Post by Nomasn » Fri, 29 Sep 2006 02:10:28


As the subject says, when I call Items.Clear() for a ComboBox the text
inside the edit of the ComboBox is selected with absolutely no reason.
Is there a way to avoid this (bug)? thanks
 
 
 

ComboBox.Items.Clear() causes the selection of Text inside the edit of the ComboBox

Post by Nomasn » Fri, 29 Sep 2006 02:32:40


I think I found the bug. When you call the Items.Clear, the
ObjectCollection inside the ComboBox call the NativeClear method of the
ComboBox itself:

private void NativeClear()
{
string text1 = null;
if (this.DropDownStyle != ComboBoxStyle.DropDownList)
{
text1 = this.WindowText;
}
base.SendMessage(0x14b, 0, 0);
if (text1 != null)
{
this.WindowText = text1;
}
}

as you can see the NativeClear send the message 0x14b that is the
CB_RESETCONTENT message and (from msdn) "An application sends a
CB_RESETCONTENT message to remove all items from the list box and edit
control of a combo box". So the NativeClear restore the text value if
the ComboBox is a DropDownList, that is, a editable ComboBox. I think
that when the method set the WindowText, for some reason the text is
selected. But WHY? it does not make sense.