21

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.

MattBH
  • 1,562
  • 3
  • 24
  • 31

15 Answers15

12

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

combobox1.SelectionLength = 0;
Scott
  • 11,046
  • 10
  • 51
  • 83
  • 1
    Doing this and I can't highlight the text later. Its getting deselected every time because the OnPaint method gets called often – Martin Fernau Oct 24 '14 at 16:00
3

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.

Smitty
  • 63
  • 1
  • 1
  • 7
3

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;
shivam
  • 16,048
  • 3
  • 56
  • 71
3

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(); 
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
sll
  • 61,540
  • 22
  • 104
  • 156
  • it's weird when i click on it after its loaded and then click on something else then that focus thing works!??! it's driving me nuts – MattBH Oct 27 '11 at 13:42
  • does your combobox is bound to some data source? If yes, try SelectedIndex = -1 – sll Oct 27 '11 at 13:48
  • it is bound, but i then choose an item from the list. It's basically a form that's being prepopulated from a db, so i need to select the correct item. – MattBH Oct 27 '11 at 13:52
2

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

combo.selectedindex = -1;
gurrawar
  • 363
  • 2
  • 8
  • 17
  • 4
    Thanks, but i'm not trying to change the selected item, i just want to un-highlight the text. – MattBH Jul 05 '12 at 07:08
2

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; }); };
Ola Berntsson
  • 647
  • 1
  • 6
  • 12
1

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.

Alex
  • 1,574
  • 17
  • 36
1

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);
Geograph
  • 2,274
  • 23
  • 23
1

'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.

Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
FranD
  • 11
  • 1
0

Here is what I do:

private void faceComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
     this.ActiveControl = cancelButton;
}
Cesar A. Rivas
  • 1,355
  • 1
  • 10
  • 13
0

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

Aedna
  • 761
  • 6
  • 5
0

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();
};
P. Av.
  • 1
  • 1
0

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;
}
Martin Wantke
  • 4,287
  • 33
  • 21
0

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:)

spoon
  • 1
-1

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!