Forcibly Suggesting Combo Box Auto Complete Mode To None - C# WinForms

863 Views Asked by At

I am using Metro Frame Work Combo Box in my WinForm,

When I try to set cmbCACName.AutoCompleteMode = AutoCompleteMode.Append; it is throwing an exception like 'AutoCompleteMode.None can be used when DropDownStyle is ComboBoxStyle.DropDownList'.

Here is my code

cmbCACName.DropDownStyle = ComboBoxStyle.Simple;
cmbCACName.AutoCompleteMode = AutoCompleteMode.Append;
cmbCACName.AutoCompleteSource = AutoCompleteSource.ListItems;

Here is an exception: enter image description here

I could not understand what is happening.

Thanks in advance

1

There are 1 best solutions below

7
Reza Aghaei On

Here is the reason for the exception:

  • In metro framework, the DropDownStyle property of the MetroComboBox has been overridden to always set DropDownStyle to DropDownList.

  • In other hand, in ComboBox, the AutoCompleteMode property contains a validation rule, to throw an exception whenever the value of AutoCompleteMode is set to a value other than None.

So, the first line of your code is technically setting DropDownStyle to DropDownList. So in the second line, assigning Append to AutoCompleteMode will result in an exception.

MetroComboBox.DropDownList

[DefaultValue(ComboBoxStyle.DropDownList)]
[Browsable(false)]
public new ComboBoxStyle DropDownStyle
{
    get { return ComboBoxStyle.DropDownList; }
    set { base.DropDownStyle = ComboBoxStyle.DropDownList; }
}

ComboBox.AutoCompleteMode

DefaultValue(AutoCompleteMode.None),
SRDescription(SR.ComboBoxAutoCompleteModeDescr),
Browsable(true), EditorBrowsable(EditorBrowsableState.Always)
]
public AutoCompleteMode AutoCompleteMode {
    get {
        return autoCompleteMode;
    }
    set {
        //valid values are 0x0 to 0x3
        if (!ClientUtils.IsEnumValid(value, (int)value, (int)AutoCompleteMode.None, (int)AutoCompleteMode.SuggestAppend)) {
            throw new InvalidEnumArgumentException("value", (int)value, typeof(AutoCompleteMode));
        }
        if (this.DropDownStyle == ComboBoxStyle.DropDownList &&
            this.AutoCompleteSource != AutoCompleteSource.ListItems &&
            value != AutoCompleteMode.None) {
            throw new NotSupportedException(SR.GetString(SR.ComboBoxAutoCompleteModeOnlyNoneAllowed));
        }
        if (Application.OleRequired() != System.Threading.ApartmentState.STA) {
            throw new ThreadStateException(SR.GetString(SR.ThreadMustBeSTA));
        }
        bool resetAutoComplete = false;
        if (autoCompleteMode != AutoCompleteMode.None && value == AutoCompleteMode.None) {
            resetAutoComplete = true;
        }
        autoCompleteMode = value;
        SetAutoComplete(resetAutoComplete, true);
    }
}