| | 1 | | using PropertyGridHelpers.Support; |
| | 2 | | using PropertyGridHelpers.UIEditors; |
| | 3 | | using System; |
| | 4 | | using System.ComponentModel; |
| | 5 | | using System.Drawing.Design; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Windows.Forms; |
| | 8 | |
|
| | 9 | | namespace PropertyGridHelpers.Controls |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A specialized <see cref="ComboBox"/> designed for use in a <see cref="PropertyGrid"/> |
| | 13 | | /// drop-down editor, supporting auto-completion and value commitment handling. |
| | 14 | | /// </summary> |
| | 15 | | /// <remarks> |
| | 16 | | /// This control is intended for use in property editors where a user-friendly |
| | 17 | | /// auto-complete experience is desired, such as file paths, URLs, or custom lists. |
| | 18 | | /// The control implements <see cref="IDropDownEditorControl"/>, allowing it to be hosted |
| | 19 | | /// by a <see cref="UITypeEditor"/> like <see cref="UIEditors.DropDownVisualizer{TControl}"/>. |
| | 20 | | /// </remarks> |
| | 21 | | /// <seealso cref="ComboBox"/> |
| | 22 | | /// <seealso cref="IDropDownEditorControl"/> |
| | 23 | | public class AutoCompleteComboBox |
| | 24 | | : ComboBox, IDropDownEditorControl |
| | 25 | | { |
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="AutoCompleteComboBox"/> class. |
| | 28 | | /// </summary> |
| | 29 | | /// <remarks> |
| | 30 | | /// Default settings: |
| | 31 | | /// <list type="bullet"> |
| | 32 | | /// <item><description><see cref="ComboBox.DropDownStyle"/> = <see cref="ComboBoxStyle.DropDown"/></description> |
| | 33 | | /// <item><description><see cref="ComboBox.AutoCompleteMode"/> = <see cref="AutoCompleteMode.SuggestAppend"/></d |
| | 34 | | /// <item><description><see cref="ComboBox.AutoCompleteSource"/> = <see cref="AutoCompleteSource.CustomSource"/> |
| | 35 | | /// </list> |
| | 36 | | /// </remarks> |
| 88 | 37 | | public AutoCompleteComboBox() |
| 80 | 38 | | { |
| 88 | 39 | | DropDownStyle = ComboBoxStyle.DropDown; |
| 88 | 40 | | AutoCompleteMode = AutoCompleteMode.SuggestAppend; |
| 88 | 41 | | AutoCompleteSource = AutoCompleteSource.CustomSource; |
| 88 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets or sets the value displayed and edited in the combo box. |
| | 46 | | /// </summary> |
| | 47 | | /// <value> |
| | 48 | | /// The current text of the combo box. |
| | 49 | | /// </value> |
| | 50 | | /// <remarks> |
| | 51 | | /// This property is used by <see cref="DropDownVisualizer{TControl}"/> to transfer |
| | 52 | | /// data between the editor and the hosting environment. |
| | 53 | | /// </remarks> |
| | 54 | | [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] |
| | 55 | | public object Value |
| | 56 | | { |
| | 57 | | get |
| 32 | 58 | | { |
| 40 | 59 | | if (SelectedItem is ItemWrapper<object> wrapper) |
| 24 | 60 | | return wrapper.Value; |
| 32 | 61 | | return Text; // fallback |
| 32 | 62 | | } |
| | 63 | | set |
| 32 | 64 | | { |
| 40 | 65 | | if (value != null) |
| 28 | 66 | | { |
| | 67 | | // Try to find the matching item wrapper |
| 132 | 68 | | foreach (var item in Items) |
| 40 | 69 | | { |
| 48 | 70 | | if (item is ItemWrapper<object> wrapper && Equals(wrapper.Value, value)) |
| 16 | 71 | | { |
| 24 | 72 | | SelectedItem = wrapper; |
| 24 | 73 | | return; |
| | 74 | | } |
| 32 | 75 | | } |
| | 76 | |
|
| | 77 | | // Fallback to setting raw text |
| 28 | 78 | | Text = value.ToString(); |
| 20 | 79 | | } |
| 40 | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Gets or sets the type descriptor context in which the control is being used. |
| | 85 | | /// </summary> |
| | 86 | | [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] |
| | 87 | | public ITypeDescriptorContext Context |
| | 88 | | { |
| 4 | 89 | | get; |
| 20 | 90 | | set; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Gets or sets the culture to use for localization or formatting. |
| | 95 | | /// </summary> |
| | 96 | | [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] |
| | 97 | | public CultureInfo Culture |
| | 98 | | { |
| 4 | 99 | | get; |
| 20 | 100 | | set; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Occurs when the user finishes editing and the value should be committed. |
| | 105 | | /// </summary> |
| | 106 | | /// <remarks> |
| | 107 | | /// This event is used to close the drop-down when editing is complete. |
| | 108 | | /// It is raised when either: |
| | 109 | | /// <list type="bullet"> |
| | 110 | | /// <item><description>The selected value changes</description></item> |
| | 111 | | /// <item><description>The control loses focus</description></item> |
| | 112 | | /// </list> |
| | 113 | | /// </remarks> |
| 136 | 114 | | public event EventHandler ValueCommitted = delegate { }; |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Raises the <see cref="ListControl.SelectedValueChanged"/> event and notifies that editing is complete. |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="e">Event data.</param> |
| | 120 | | protected override void OnSelectedValueChanged(EventArgs e) |
| 28 | 121 | | { |
| 36 | 122 | | base.OnSelectedValueChanged(e); |
| 36 | 123 | | ValueCommitted.Invoke(this, EventArgs.Empty); |
| 36 | 124 | | } |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Raises the <see cref="Control.Leave"/> event and notifies that editing is complete. |
| | 128 | | /// </summary> |
| | 129 | | /// <param name="e">Event data.</param> |
| | 130 | | protected override void OnLeave(EventArgs e) |
| 12 | 131 | | { |
| 20 | 132 | | base.OnLeave(e); |
| 20 | 133 | | ValueCommitted.Invoke(this, EventArgs.Empty); |
| 20 | 134 | | } |
| | 135 | | } |
| | 136 | | } |