C # ComboBox disable selection

I have a custom combobox where DropDownStyle = ComboBoxStyle.DropDown;. The DropDown style is set because I want to set the Text ComboBox property for something outside the list of values. Everything works well, except that the ComboBox highlights the text when it leaves, and when I click on combobox editing is available. How can I handle this? To illustrate:

enter image description here

The first image is where everything looks good, the second is the backlight situation, the third editing is on.

+4
source share
6 answers

Try deselecting the text after closing DropDown:

void comboBox1_DropDownClosed(object sender, EventArgs e) {
  this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
}
+8
source

. Resize. , - , , , , . ( , ComboBox; , , , , , ComboBox.)

comboBox.Resize += (s, e) => {
   if (!comboBox.IsHandleCreated)
      return;  // avoid possible exception

   comboBox.BeginInvoke(new Action(() => comboBox.SelectionLength = 0));
};

, , ? , , -, Resize, Resize , . ( , , , .)

BeginInvoke , Resize , IsHandleCreated , BeginInvoke .

, , . , , , , .

comboBox.Resize += (s, e) => {
   if (!comboBox.IsHandleCreated)
      return;

   comboBox.BeginInvoke(new Action(() => {
      var parent = comboBox.FindForm();
      if (parent == null)
         return;

      if (parent.ActiveControl == null)
         return;

      if (parent.ActiveControl == comboBox)
         return;

      comboBox.SelectionLength = 0;
   }));
};

, "" , , . Resize , BeginInvoke , . Queue Stack, ( , , ).

+2

, DropdownStyle DropdownList.

yourComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
+1

, :

  • DropdownStyle DropdownList
  • this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
  • combobox1.SelectionLength = 0;
  • comboBox.TabIndex
  • SendKeys.Send("{ESC}");,

. , Label:

    label.Focus();

.

0

, , combobox.

: combobox, , .

, autobox auto , .

: , :

<ComboBox IsTextSearchEnabled="False" IsEditable="True" x:Name="CMB_ClientName"/>
<TextBox Text="{Binding ElementName=CMB_ClientName, Path=Text}" TextChanged="ComboBoxChange" x:Name="TXT_ClientName"/>

TextChanged:

private void ComboBoxChange(object sender, TextChangedEventArgs e)
    {
        //Clear ComboBox items
        CMB_ClientName.Items.Clear();
        //Auto Open DropDownList
        CMB_ClientName.IsDropDownOpen = true;

        //Get data from database (use entity framework 6.x)
        dbEntity.Client.Load();

        //Attribute Data to variable
        var clients = dbEntity.Client.Local;


        foreach (Client client in clients)
        {
            //If data begin with the texbox text, the data is add to the combobox items list.
            if (client.Nom.ToLower().StartsWith(TXT_NomClient.Text.ToLower()))
            {
                CMB_ClientName.Items.Add(client.Nom);
            }
        }
    }

, , , , .

, , .

Math.

Ps: , . , .

0

( , ), tabobop combobox false. , , , false .

0

All Articles