Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Hiding selected text in a ComboBox?

When a Windows Form is populated, any data-bound ComboBoxes show text as selected, even though they do not have focus.  Tabbing thru each ComboBox, so it receives GotFocus & LostFocus will remove the highlighting.  However, it doesn't seem like this should be necessary.

Does anyone know how to clear the highlights as soon as the form is populated, without cycling focus on every ComboBox?

John Lemberger
Thursday, April 10, 2003

the ComboBox has public SelectionLength and SelectionStart properties. You could set both of these to 0 in the form constructor. I imagine this would solve your problem.



Pete Hodgson
Monday, April 21, 2003

Setting SelectionStart & SelectionLength should solve the problem, but 'when' do you call them?  The constructor is too early, because DataBinding hasn't populated the control yet.  And,  I'm interested in a ComboBox control that takes care of this problem internally.  IMO, it shouldn't be necessary to add workaround code to every form which uses ComboBoxes. 

John Lemberger
Monday, April 21, 2003

How about handling the Binding.Format event for the binding associated with the ComboBox?

The thing is, you'd need to also handle the ControlBindingsCollection.CollectionChanged event in order to attach a handler to each Binding.Format as it was added to the control's DataBindings.

That sounds a bit fiddly to me, but it's the only thing I can think of offhand. There's probably a cleaner way to do it though. It might be worth watching what events are fired by the ComboBox as it is initialised, and seeing if there's a better way to do this.

Pete Hodgson
Tuesday, April 22, 2003

*  Recent Topics

*  Fog Creek Home