/// <summary> /// Enables auto suggest for the specified text box. /// </summary> /// <param name="textBox">The text box.</param> /// <param name="suggestions">The suggestions.</param> public static void Enable(TextBox textBox, string[] suggestions) { // Try to enable a more advanced settings for AutoComplete via the WinShell interface try { SourceCustomList source = new SourceCustomList { StringList = suggestions.ToArray() }; // For options descriptions see: // https://docs.microsoft.com/en-us/windows/win32/api/shldisp/ne-shldisp-autocompleteoptions AUTOCOMPLETEOPTIONS options = AUTOCOMPLETEOPTIONS.ACO_UPDOWNKEYDROPSLIST | AUTOCOMPLETEOPTIONS.ACO_USETAB | AUTOCOMPLETEOPTIONS.ACO_AUTOAPPEND | AUTOCOMPLETEOPTIONS.ACO_AUTOSUGGEST; Enable(textBox.Handle, source, options); } catch (Exception) { // In case of an error, let's fall back to the default AutoCompleteStringCollection source = new AutoCompleteStringCollection(); source.AddRange(suggestions); textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox.AutoCompleteCustomSource = source; } }
/// <summary> /// Enables the specified control handle. /// </summary> /// <param name="controlHandle">The control handle.</param> /// <param name="items">The items.</param> /// <param name="options">The options.</param> private static void Enable(IntPtr controlHandle, SourceCustomList items, AUTOCOMPLETEOPTIONS options) { if (controlHandle == IntPtr.Zero) { return; } IAutoComplete2?iac = null; try { iac = GetAutoComplete() as IAutoComplete2; iac?.Init(controlHandle, items, string.Empty, string.Empty); iac?.SetOptions(options); iac?.Enable(true); } finally { if (iac != null) { Marshal.ReleaseComObject(iac); } } }