How to deselect the text of a combobox

36.6k views Asked by At

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.

16

There are 16 answers

3
sll On

Try this out

combo.SelectedText = String.Empty;

Regarding your problem with focus: (MSDN)

When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected

So strange; why the following didn't work:

anotherControl.Focus(); 
0
spoon On

Not sure what you might be doing in the background ie fired events etc. However in the combox selectedindexchanged event you can add anotherControl.Select().

That should:)

0
Ola Berntsson On

I know, ancient post, ancient technology, but this ugly oneliner worked for me:

cb.SelectedValueChanged += (s, e) => { cb.BeginInvoke((MethodInvoker)delegate { cb.SelectionStart = cb.Text.Length; }); };
0
Martin Wantke On

Subscribe to the "SelectedIndexChanged" event, which has the task to set the focus of the ComboBox.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox1.Focus();
}

Subscribe to the "Paint" event from the main form. The deselection takes place in this event handler:

private void MainForm_Paint(object sender, PaintEventArgs e)
{
    comboBox1.SelectionLength = 0;
}
1
Scott On

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

combobox1.SelectionLength = 0;
0
Alex On

You did not specify whether the user is supposed to be able to edit the ComboBox values or not. If not, then DropDownStyle of the ComboBox should be set to ComboBoxStyle.DropDownList. This way the ComboBox will only allow selecting existing values, users will not be able to type new values and the text will never be highlighted.

1
Aedna On

these 3 lines helped me:

cbInstallationType.SelectionStart = 0;
cbInstallationType.SelectionLength = 0;
cbInstallationType.TabIndex = 99;

TabIndex has to be not the first one, so that it is not the first item in the form

0
FranD On

'Select' doesn't work for me. But I found a very simple trick. Add this right into the SelectedIndexChanged-Event:

comboBox1.Hide();
comboBox1.Show();

Works for me.

1
Murtuza Ali Khan Mohammed On

You simply have to place this code on generated event or button click where you want to deselect the text of a Combo Box

ComboBox1.SelectedItem = null;
1
Сергей Мухин On

Use Style --> csOwnerDrawFixed.

2
Daniel Yahdav On

I don't care how old this is. We're building a knowledge base.

I use D5 (and probably prior versions are the same). You need to use:

Combobox.SelLength :=  0; 

Also, if that doesn't work, this one does: Avoid the default Style (csDropDown). And set it to:

Combobox.Style := csDropDownList; 

as Alex suggested.

Thank you. Works great!

0
Cesar A. Rivas On

Here is what I do:

private void faceComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
     this.ActiveControl = cancelButton;
}
1
gurrawar On

I know its been a while since you asked this question. But here is what you can do

combo.selectedindex = -1;
0
Geograph On

On event SelectedIndexChanged add the timer which will be executed only once after 10 milliseconds.

new System.Threading.Timer((s) =>
            {
                comboBox1.Invoke(new Action(() =>
                {
                    comboBox1.Select(0, 0);
                }));
            }, null, 10, System.Threading.Timeout.Infinite);
0
Smitty On

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.

0
P. Av. On

In my case selecting appeared after resize. This solved it:

textBox.Resize += (sender, args) =>
{
    Control c = sender as Control;
    if (c == null || c.Parent == null)
        return;

    c.Focus();
    c.Parent.Focus();
};