MattBH
MattBH

Reputation: 1642

How to deselect the text of a combobox

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.

Upvotes: 23

Views: 37991

Answers (16)

Use Style --> csOwnerDrawFixed.

Upvotes: -1

Ola Berntsson
Ola Berntsson

Reputation: 1236

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; }); };

Upvotes: 2

FranD
FranD

Reputation: 21

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

Upvotes: 2

Martin Wantke
Martin Wantke

Reputation: 4571

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;
}

Upvotes: 0

Geograph
Geograph

Reputation: 2604

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

Upvotes: 1

P. Av.
P. Av.

Reputation: 1

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();
};

Upvotes: 0

Daniel Yahdav
Daniel Yahdav

Reputation: 63

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!

Upvotes: -1

sll
sll

Reputation: 62544

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(); 

Upvotes: 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;

Upvotes: 4

Aedna
Aedna

Reputation: 789

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

Upvotes: 0

Scott
Scott

Reputation: 11206

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

combobox1.SelectionLength = 0;

Upvotes: 12

Alex
Alex

Reputation: 1684

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.

Upvotes: 1

Cesar A. Rivas
Cesar A. Rivas

Reputation: 1355

Here is what I do:

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

Upvotes: 0

Smitty
Smitty

Reputation: 63

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.

Upvotes: 3

gurrawar
gurrawar

Reputation: 363

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

combo.selectedindex = -1;

Upvotes: 2

spoon
spoon

Reputation: 1

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

Upvotes: 0

Related Questions