| | | 1 | | using System; |
| | | 2 | | using System.ComponentModel; |
| | | 3 | | using System.Drawing.Design; |
| | | 4 | | using System.Windows.Forms; |
| | | 5 | | |
| | | 6 | | namespace PropertyGridHelpers.Attributes |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Specifies auto-complete behavior for a string property in a <see cref="PropertyGrid"/>, |
| | | 10 | | /// providing suggestions based on built-in sources, a custom string list, an enum, or a static provider. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// This attribute is used in conjunction with a custom <see cref="UITypeEditor"/> (e.g., <c>AutoCompleteComboBoxEdi |
| | | 14 | | /// to configure how suggestions are shown to the user at design-time in a property grid editor. |
| | | 15 | | /// |
| | | 16 | | /// Auto-complete behavior includes: |
| | | 17 | | /// <list type="bullet"> |
| | | 18 | | /// <item> |
| | | 19 | | /// <term><see cref="AutoCompleteSource"/></term> |
| | | 20 | | /// <description> |
| | | 21 | | /// The source of the suggestions, such as file system, history list, or custom-defined values. |
| | | 22 | | /// </description> |
| | | 23 | | /// </item> |
| | | 24 | | /// <item> |
| | | 25 | | /// <term><see cref="AutoCompleteMode"/></term> |
| | | 26 | | /// <description> |
| | | 27 | | /// Defines how the suggestions are shown—inline (Append), in a dropdown (Suggest), or both. |
| | | 28 | | /// </description> |
| | | 29 | | /// </item> |
| | | 30 | | /// <item> |
| | | 31 | | /// <term><see cref="DropDownStyle"/></term> |
| | | 32 | | /// <description> |
| | | 33 | | /// Controls whether the editor is editable (DropDown), fixed (DropDownList), or simple. |
| | | 34 | | /// </description> |
| | | 35 | | /// </item> |
| | | 36 | | /// <item> |
| | | 37 | | /// <term><see cref="Values"/></term> |
| | | 38 | | /// <description> |
| | | 39 | | /// If <see cref="AutoCompleteSource"/> is <c>CustomSource</c>, this holds the actual list of values used for sugges |
| | | 40 | | /// </description> |
| | | 41 | | /// </item> |
| | | 42 | | /// </list> |
| | | 43 | | /// </remarks> |
| | | 44 | | /// <example> |
| | | 45 | | /// Use with built-in file system suggestions: |
| | | 46 | | /// <code> |
| | | 47 | | /// [AutoCompleteSetup(AutoCompleteSource.FileSystem)] |
| | | 48 | | /// public string FilePath { get; set; } |
| | | 49 | | /// </code> |
| | | 50 | | /// Use with custom string values: |
| | | 51 | | /// <code> |
| | | 52 | | /// [AutoCompleteSetup("Red", "Green", "Blue")] |
| | | 53 | | /// public string ColorName { get; set; } |
| | | 54 | | /// </code> |
| | | 55 | | /// Use with enum names: |
| | | 56 | | /// <code> |
| | | 57 | | /// [AutoCompleteSetup(typeof(KnownFruits))] |
| | | 58 | | /// public string FavoriteFruit { get; set; } |
| | | 59 | | /// </code> |
| | | 60 | | /// Use with static string provider: |
| | | 61 | | /// <code> |
| | | 62 | | /// public static class KnownUsers |
| | | 63 | | /// { |
| | | 64 | | /// public static string[] Values => new[] { "Admin", "Editor", "Viewer" }; |
| | | 65 | | /// } |
| | | 66 | | /// |
| | | 67 | | /// [AutoCompleteSetup(typeof(KnownUsers))] |
| | | 68 | | /// public string Role { get; set; } |
| | | 69 | | /// </code> |
| | | 70 | | /// </example> |
| | | 71 | | /// <seealso cref="System.Windows.Forms.AutoCompleteSource"/> |
| | | 72 | | /// <seealso cref="System.Windows.Forms.AutoCompleteMode"/> |
| | | 73 | | /// <seealso cref="ComboBoxStyle"/> |
| | | 74 | | /// <seealso cref="UITypeEditor"/> |
| | | 75 | | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] |
| | | 76 | | public class AutoCompleteSetupAttribute : Attribute |
| | | 77 | | { |
| | | 78 | | /// <summary> |
| | | 79 | | /// Defines the mode that determines how auto-complete suggestions are provided. |
| | | 80 | | /// </summary> |
| | | 81 | | public enum SourceMode |
| | | 82 | | { |
| | | 83 | | /// <summary> |
| | | 84 | | /// Indicates that the auto-complete suggestions are supplied directly via a values array. |
| | | 85 | | /// </summary> |
| | | 86 | | Values, |
| | | 87 | | /// <summary> |
| | | 88 | | /// Indicates that the auto-complete suggestions are retrieved from a provider type, |
| | | 89 | | /// such as an enum or a class with a static string array property. |
| | | 90 | | /// </summary> |
| | | 91 | | Provider |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets the source from which auto-complete suggestions are retrieved. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <remarks> |
| | | 98 | | /// Controls where the combo box obtains its suggestions: |
| | | 99 | | /// <list type="bullet"> |
| | | 100 | | /// <item><description><see cref="AutoCompleteSource.FileSystem"/> – File and directory paths.</description></ |
| | | 101 | | /// <item><description><see cref="AutoCompleteSource.HistoryList"/> – URL history.</description></item> |
| | | 102 | | /// <item><description><see cref="AutoCompleteSource.CustomSource"/> – User-supplied values via <see cref="Val |
| | | 103 | | /// </list> |
| | | 104 | | /// </remarks> |
| | | 105 | | /// <seealso cref="System.Windows.Forms.AutoCompleteSource"/> |
| | | 106 | | public AutoCompleteSource AutoCompleteSource |
| | | 107 | | { |
| | 356 | 108 | | get; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets the mode that determines how suggestions appear in the UI as the user types. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <remarks> |
| | | 115 | | /// Controls the behavior of suggestion display: |
| | | 116 | | /// <list type="bullet"> |
| | | 117 | | /// <item><description><see cref="AutoCompleteMode.Suggest"/> – Shows a list of matches.</description></item> |
| | | 118 | | /// <item><description><see cref="AutoCompleteMode.Append"/> – Auto-completes the typed text with the closest |
| | | 119 | | /// <item><description><see cref="AutoCompleteMode.SuggestAppend"/> – Combines suggest and append modes.</desc |
| | | 120 | | /// </list> |
| | | 121 | | /// </remarks> |
| | | 122 | | /// <seealso cref="System.Windows.Forms.AutoCompleteMode"/> |
| | | 123 | | public AutoCompleteMode AutoCompleteMode |
| | | 124 | | { |
| | 156 | 125 | | get; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets the style of the drop-down editor used in the UI. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <remarks> |
| | | 132 | | /// Controls how the user can edit and interact with the drop-down: |
| | | 133 | | /// <list type="bullet"> |
| | | 134 | | /// <item><description><see cref="ComboBoxStyle.DropDown"/> – Editable text box with a drop-down list.</descri |
| | | 135 | | /// <item><description><see cref="ComboBoxStyle.DropDownList"/> – Drop-down list only, no free-form input allo |
| | | 136 | | /// <item><description><see cref="ComboBoxStyle.Simple"/> – Always visible list with an editable text box.</de |
| | | 137 | | /// </list> |
| | | 138 | | /// </remarks> |
| | | 139 | | /// <seealso cref="ComboBoxStyle"/> |
| | | 140 | | public ComboBoxStyle DropDownStyle |
| | | 141 | | { |
| | 148 | 142 | | get; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets the set of custom string values used for auto-complete suggestions |
| | | 147 | | /// when <see cref="AutoCompleteSource"/> is set to <c>CustomSource</c>. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <remarks> |
| | | 150 | | /// These values can be specified: |
| | | 151 | | /// <list type="bullet"> |
| | | 152 | | /// <item><description>Directly via the attribute constructor.</description></item> |
| | | 153 | | /// <item><description>Derived from an <c>enum</c> type (using its names).</description></item> |
| | | 154 | | /// <item><description>Loaded from a class with a public static <c>string[] Values</c> property.</description> |
| | | 155 | | /// </list> |
| | | 156 | | /// </remarks> |
| | | 157 | | |
| | | 158 | | public string[] Values |
| | | 159 | | { |
| | 148 | 160 | | get; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Gets the type that provides the source of auto-complete values when |
| | | 165 | | /// <see cref="AutoCompleteSource"/> is set to <c>CustomSource</c>. |
| | | 166 | | /// Typically, this can be an enum type or a class with a static <c>string[] Values</c> property. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <value> |
| | | 169 | | /// A <see cref="Type"/> representing the provider of suggestions for the editor, |
| | | 170 | | /// or <c>null</c> if not specified. |
| | | 171 | | /// </value> |
| | | 172 | | public Type ProviderType |
| | | 173 | | { |
| | 160 | 174 | | get; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets the source mode used to determine how auto-complete values were initialized |
| | | 179 | | /// for this attribute. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <value> |
| | | 182 | | /// A <see cref="SourceMode"/> enumeration value indicating how the values |
| | | 183 | | /// were resolved (e.g., via direct array, enum, or provider type). |
| | | 184 | | /// </value> |
| | | 185 | | public SourceMode Mode |
| | | 186 | | { |
| | 192 | 187 | | get; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <param name="autoCompleteSource">Sets <see cref="AutoCompleteSource"/>.</param> |
| | | 194 | | public AutoCompleteSetupAttribute( |
| | | 195 | | AutoCompleteSource autoCompleteSource = AutoCompleteSource.None) |
| | 52 | 196 | | : this(AutoCompleteMode.SuggestAppend, autoCompleteSource, ComboBoxStyle.DropDown) { } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 200 | | /// </summary> |
| | | 201 | | /// <param name="autoCompleteSource">Sets <see cref="AutoCompleteSource"/>.</param> |
| | | 202 | | /// <param name="dropDownStyle">Sets <see cref="DropDownStyle"/>.</param> |
| | | 203 | | public AutoCompleteSetupAttribute( |
| | | 204 | | AutoCompleteSource autoCompleteSource, |
| | | 205 | | ComboBoxStyle dropDownStyle) |
| | 28 | 206 | | : this(AutoCompleteMode.SuggestAppend, autoCompleteSource, dropDownStyle) { } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="autoCompleteMode">Sets <see cref="AutoCompleteMode"/>.</param> |
| | | 212 | | /// <param name="autoCompleteSource">Sets <see cref="AutoCompleteSource"/>.</param> |
| | | 213 | | /// <param name="dropDownStyle">Sets <see cref="DropDownStyle"/>.</param> |
| | | 214 | | public AutoCompleteSetupAttribute( |
| | | 215 | | AutoCompleteMode autoCompleteMode, |
| | | 216 | | AutoCompleteSource autoCompleteSource, |
| | | 217 | | ComboBoxStyle dropDownStyle) |
| | | 218 | | #if NET35 || NET452 |
| | 4 | 219 | | : this(autoCompleteMode, autoCompleteSource, dropDownStyle, new string[0]) { } |
| | | 220 | | #elif NET5_0_OR_GREATER |
| | 48 | 221 | | : this(autoCompleteMode, autoCompleteSource, dropDownStyle, []) { } |
| | | 222 | | #else |
| | 12 | 223 | | : this(autoCompleteMode, autoCompleteSource, dropDownStyle, Array.Empty<string>()) { } |
| | | 224 | | #endif |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 228 | | /// </summary> |
| | | 229 | | /// <param name="values">Sets <see cref="Values"/>.</param> |
| | | 230 | | public AutoCompleteSetupAttribute( |
| | | 231 | | params string[] values) |
| | 52 | 232 | | : this(AutoCompleteMode.SuggestAppend, ComboBoxStyle.DropDown, values) { } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 236 | | /// </summary> |
| | | 237 | | /// <param name="dropDownStyle">Sets <see cref="DropDownStyle"/>.</param> |
| | | 238 | | /// <param name="values">Sets <see cref="Values"/>.</param> |
| | | 239 | | public AutoCompleteSetupAttribute( |
| | | 240 | | ComboBoxStyle dropDownStyle, |
| | | 241 | | params string[] values) |
| | 28 | 242 | | : this(AutoCompleteMode.SuggestAppend, dropDownStyle, values) { } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 246 | | /// </summary> |
| | | 247 | | /// <param name="autoCompleteSource">Sets <see cref="AutoCompleteSource"/>.</param> |
| | | 248 | | /// <param name="autoCompleteMode">Sets <see cref="AutoCompleteMode"/>.</param> |
| | | 249 | | /// <param name="values">Sets <see cref="Values"/>.</param> |
| | | 250 | | public AutoCompleteSetupAttribute( |
| | | 251 | | AutoCompleteSource autoCompleteSource, |
| | | 252 | | AutoCompleteMode autoCompleteMode, |
| | | 253 | | params string[] values) |
| | 52 | 254 | | : this(autoCompleteMode, autoCompleteSource, ComboBoxStyle.DropDown, values) { } |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 258 | | /// </summary> |
| | | 259 | | /// <param name="autoCompleteMode">Sets <see cref="AutoCompleteMode"/>.</param> |
| | | 260 | | /// <param name="values">Sets <see cref="Values"/>.</param> |
| | | 261 | | public AutoCompleteSetupAttribute( |
| | | 262 | | AutoCompleteMode autoCompleteMode, |
| | | 263 | | params string[] values) |
| | 28 | 264 | | : this(autoCompleteMode, ComboBoxStyle.DropDown, values) { } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 268 | | /// </summary> |
| | | 269 | | /// <param name="autoCompleteMode">Sets <see cref="AutoCompleteMode"/>.</param> |
| | | 270 | | /// <param name="dropDownStyle">Sets <see cref="DropDownStyle"/>.</param> |
| | | 271 | | /// <param name="values">Sets <see cref="Values"/>.</param> |
| | | 272 | | public AutoCompleteSetupAttribute( |
| | | 273 | | AutoCompleteMode autoCompleteMode, |
| | | 274 | | ComboBoxStyle dropDownStyle, |
| | | 275 | | params string[] values) |
| | 76 | 276 | | : this(autoCompleteMode, AutoCompleteSource.CustomSource, dropDownStyle, values) { } |
| | | 277 | | |
| | | 278 | | /// <summary> |
| | | 279 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 280 | | /// </summary> |
| | | 281 | | /// <param name="autoCompleteMode">Sets <see cref="AutoCompleteMode"/>.</param> |
| | | 282 | | /// <param name="autoCompleteSource">Sets <see cref="AutoCompleteSource"/>.</param> |
| | | 283 | | /// <param name="dropDownStyle">Sets <see cref="DropDownStyle"/>.</param> |
| | | 284 | | /// <param name="values">Sets <see cref="Values"/>.</param> |
| | | 285 | | /// <exception cref="ArgumentException">At least one auto-complete value must be provided.</exception> |
| | 64 | 286 | | public AutoCompleteSetupAttribute( |
| | 64 | 287 | | AutoCompleteMode autoCompleteMode, |
| | 64 | 288 | | AutoCompleteSource autoCompleteSource, |
| | 64 | 289 | | ComboBoxStyle dropDownStyle, |
| | 64 | 290 | | params string[] values) |
| | 56 | 291 | | { |
| | 64 | 292 | | Console.WriteLine("🚨 AutoCompleteSetupAttribute values ctor called!"); |
| | 64 | 293 | | AutoCompleteMode = autoCompleteMode; |
| | 64 | 294 | | DropDownStyle = dropDownStyle; |
| | 64 | 295 | | AutoCompleteSource = autoCompleteSource; |
| | 64 | 296 | | Values = values; |
| | | 297 | | |
| | 64 | 298 | | Mode = SourceMode.Values; |
| | 64 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute"/> class. |
| | | 303 | | /// </summary> |
| | | 304 | | /// <param name="providerType">Sets <see cref="ProviderType"/>.</param> |
| | | 305 | | public AutoCompleteSetupAttribute( |
| | | 306 | | Type providerType) |
| | 136 | 307 | | : this(AutoCompleteMode.SuggestAppend, ComboBoxStyle.DropDown, providerType) { } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 311 | | /// </summary> |
| | | 312 | | /// <param name="autoCompleteMode">Sets <see cref="AutoCompleteMode"/>.</param> |
| | | 313 | | /// <param name="providerType">Sets <see cref="ProviderType"/>.</param> |
| | | 314 | | public AutoCompleteSetupAttribute( |
| | | 315 | | AutoCompleteMode autoCompleteMode, |
| | | 316 | | Type providerType) |
| | 28 | 317 | | : this(autoCompleteMode, ComboBoxStyle.DropDown, providerType) { } |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 321 | | /// </summary> |
| | | 322 | | /// <param name="dropDownStyle">Sets <see cref="DropDownStyle"/>.</param> |
| | | 323 | | /// <param name="providerType">Sets <see cref="ProviderType"/>.</param> |
| | | 324 | | public AutoCompleteSetupAttribute( |
| | | 325 | | ComboBoxStyle dropDownStyle, |
| | | 326 | | Type providerType) |
| | 28 | 327 | | : this(AutoCompleteMode.SuggestAppend, dropDownStyle, providerType) { } |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// Initializes a new instance of the <see cref="AutoCompleteSetupAttribute" /> class. |
| | | 331 | | /// </summary> |
| | | 332 | | /// <param name="autoCompleteMode">Sets <see cref="AutoCompleteMode"/>.</param> |
| | | 333 | | /// <param name="dropDownStyle">Sets <see cref="DropDownStyle"/>.</param> |
| | | 334 | | /// <param name="providerType">Sets <see cref="ProviderType"/>.</param> |
| | | 335 | | /// <exception cref="ArgumentNullException">providerType</exception> |
| | | 336 | | /// <exception cref="ArgumentException"> |
| | | 337 | | /// The type '{providerType.FullName}' must define a public static property named '{requiredPropertyName}'. |
| | | 338 | | /// or |
| | | 339 | | /// The '{requiredPropertyName}' property on '{providerType.FullName}' must be of type string[]. |
| | | 340 | | /// </exception> |
| | | 341 | | /// <exception cref="ArgumentNullException">providerType</exception> |
| | | 342 | | /// <exception cref="ArgumentException">The type '{providerType.FullName}' must define a public static property |
| | | 343 | | /// or |
| | | 344 | | /// The 'Values' property on '{providerType.FullName}' must be of type string[].</exception> |
| | 64 | 345 | | public AutoCompleteSetupAttribute( |
| | 64 | 346 | | AutoCompleteMode autoCompleteMode, |
| | 64 | 347 | | ComboBoxStyle dropDownStyle, |
| | 64 | 348 | | Type providerType) |
| | 56 | 349 | | { |
| | 64 | 350 | | Console.WriteLine("🚨 AutoCompleteSetupAttribute providerType ctor called!"); |
| | 64 | 351 | | AutoCompleteMode = autoCompleteMode; |
| | 64 | 352 | | DropDownStyle = dropDownStyle; |
| | 64 | 353 | | AutoCompleteSource = AutoCompleteSource.CustomSource; |
| | 64 | 354 | | ProviderType = providerType; |
| | | 355 | | |
| | 64 | 356 | | Mode = SourceMode.Provider; |
| | 64 | 357 | | } |
| | | 358 | | |
| | | 359 | | /// <summary> |
| | | 360 | | /// Get the <see cref="AutoCompleteSetupAttribute"/> for the specified context. |
| | | 361 | | /// </summary> |
| | | 362 | | /// <param name="context">The context.</param> |
| | | 363 | | /// <returns></returns> |
| | | 364 | | public static AutoCompleteSetupAttribute Get(ITypeDescriptorContext context) => |
| | 28 | 365 | | context == null || context.Instance == null || context.PropertyDescriptor == null |
| | 28 | 366 | | ? null |
| | 28 | 367 | | : Support.Support.GetFirstCustomAttribute<AutoCompleteSetupAttribute>( |
| | 28 | 368 | | Support.Support.GetPropertyInfo(context)); |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// Converts to string. |
| | | 372 | | /// </summary> |
| | | 373 | | /// <returns> |
| | | 374 | | /// A <see cref="String" /> that represents this instance. |
| | | 375 | | /// </returns> |
| | | 376 | | public override string ToString() => |
| | 104 | 377 | | AutoCompleteSource == AutoCompleteSource.CustomSource && (Mode == SourceMode.Values && Values != null) |
| | 104 | 378 | | ? $"{nameof(AutoCompleteSetupAttribute)}: {nameof(AutoCompleteMode)}={AutoCompleteMode}, {nameof(AutoCom |
| | 104 | 379 | | : AutoCompleteSource == AutoCompleteSource.CustomSource && (Mode == SourceMode.Provider && ProviderType |
| | 104 | 380 | | $"{nameof(AutoCompleteSetupAttribute)}: {nameof(AutoCompleteMode)}={AutoCompleteMode}, {nameof(AutoC |
| | 104 | 381 | | $"{nameof(AutoCompleteSetupAttribute)}: {nameof(AutoCompleteMode)}={AutoCompleteMode}, {nameof(AutoC |
| | | 382 | | } |
| | | 383 | | } |