Groups | Blog | Home
all groups > dotnet windows forms controls > may 2008 >

dotnet windows forms controls : ComboBox DropDownStyle = DropDown and selected text


Jack Jackson
5/17/2008 4:55:43 PM
When a combobox's DropDownStyle is set to DropDown (the text in the
textbox portion is editable), the combobox will select all of the text
whenever the control is repainted.

There are two unpleasant effects of this. First, all such comboboxes
that are databound and have initial values start out with their text
selected. As the user tabs through the form, the selections clear as
focus leaves each combobox.

Second, if anything is done to cause the comboboxes to be repainted,
such as minimizing and restoring the form or resizing the form when
the comboboxes are anchored such that they move, all of their text
will be selected regardless of how much if any was previously
selected.

This makes the comboboxes behave in a very different manner from
textboxes.

I have been able to mostly fix this by setting the combobox to
OwnerDraw and in the OnPaint method setting SelectionLength to 0 if
Focused is True.

Morten Wennevik [C# MVP]
5/19/2008 11:11:01 PM
Hi Jack,

I'm not sure why it does that, although I never found it problematic as a
ComboBox isn't a TextBox even though you can sometimes write in it. You can
however override the selection in the focus/enter events, but unless you keep
track of the cursor position the cursor will for the ComboBox always be at
the end.

void comboBox1_GotFocus(object sender, EventArgs e)
{
comboBox1.SelectionStart = comboBox1.Text.Length;
}

void textBox1_Enter(object sender, EventArgs e)
{
textBox1.SelectAll();
}

If you have Visual Studio 2008 you may be able to gain further information
by stepping into the ComboBox source code.

http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

--
Happy Coding!
Morten Wennevik [C# MVP]


[quoted text, click to view]
Jack Jackson
5/20/2008 7:21:45 AM
Thanks.

Putting code in GotFocus and Enter doesn't help, as I am trying to
find a way to remove the selection that the combobox does every time
it repaints whether or not the control has focus.

I finally fixed it by clearing the selection when the control gets a
WM_PAINT message in WndProc.

I find it problematic because when a form first displays all of the
comboboxes with DropDownStyle = DropDown have their text selected and
none of the textboxes do. To me that is a confusing visual effect.
Then as you tab through each combobox the selection gets removed. I'm
sure my customers would complain about that.

On Mon, 19 May 2008 23:11:01 -0700, Morten Wennevik [C# MVP]
[quoted text, click to view]
Morten Wennevik [C# MVP]
5/20/2008 9:55:00 PM
Allright, but setting SelectionStart in GotFocus behind the last character,
or setting SelectionLength to 0 would clear the selection. It won't work if
you are doing your own drawing though.

--
Happy Coding!
Morten Wennevik [C# MVP]


[quoted text, click to view]
Jack Jackson
5/21/2008 7:44:10 AM
On Tue, 20 May 2008 21:55:00 -0700, Morten Wennevik [C# MVP]
[quoted text, click to view]

Yes, that will clear it when the control receives focus. But it does
nothing for the intial display, or when the control is repainted when
it doesn't have focus (such as when the form is minimized and then
AddThis Social Bookmark Button