Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deselect the text of a combobox

Tags:

I have a krypton combo box which I data bind with a list of key-value pairs. What's happening is that when I set the selected item in code, it is highlighting the text. How can I prevent this or deselect the text?

I've tried the following:

// 1
combo.Select(0,0);
// 2
combo.Focus();
anotherControl.Focus();
// 3
combo.SelectionStart = 0;
combo.SelectionLength = combo.Text.Length;
// 4 
combo.SelectionStart = combo.Text.Length;
combo.SelectionLength = 0;

Nothing seems to work. Any help is appreciated.

like image 903
MattBH Avatar asked Oct 27 '11 13:10

MattBH


2 Answers

I managed accomplishing this be overriding the OnPaint event of my control/window and doing

combobox1.SelectionLength = 0;
like image 60
Scott Avatar answered Sep 19 '22 18:09

Scott


I may have found a solution that works:

If you are using a form, subscribe to the form's Shown event.

OR

If you are using a UserControl (like I am), you can subscribe to the VisibleChanged event.

In the event, you can then do the following:

        foreach (ComboBox cbo in (this.Controls.Cast<Control>().Where(c => c is ComboBox).Select(c => (ComboBox) c)))
        {
            cbo.SelectionLength = 0;
        }

As an aside:

I ended up having to do this for a user control in which I added ComboBoxes to the control and then needed to later dynamically set their size. Setting the size caused the highlighting that the OP was encountering.

like image 34
Smitty Avatar answered Sep 18 '22 18:09

Smitty