| | 1 | | using PropertyGridHelpers.Support; |
| | 2 | | using System; |
| | 3 | | using System.ComponentModel; |
| | 4 | | using System.Drawing.Design; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Windows.Forms; |
| | 7 | |
|
| | 8 | | namespace PropertyGridHelpers.UIEditors |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Provides a reusable, generic modal visualizer for editing a property value within a <see cref="PropertyGrid"/>. |
| | 12 | | /// This visualizer displays a modal dialog using a form of type <typeparamref name="TForm"/>, allowing |
| | 13 | | /// rich editing experiences beyond the standard in-place or dropdown editors. |
| | 14 | | /// </summary> |
| | 15 | | /// <typeparam name="TForm"> |
| | 16 | | /// A Windows Forms <see cref="Form"/> class that implements <see cref="IValueEditorForm"/> to handle |
| | 17 | | /// the editing of the property value. |
| | 18 | | /// </typeparam> |
| | 19 | | /// <remarks> |
| | 20 | | /// To use this editor, define a custom form implementing the <see cref="IValueEditorForm"/> interface. |
| | 21 | | /// The <see cref="IValueEditorForm.EditedValue"/> property is used to push the initial value |
| | 22 | | /// into the form and retrieve the edited value when the dialog is closed with <see cref="DialogResult.OK"/>. |
| | 23 | | /// </remarks> |
| | 24 | | /// <example> |
| | 25 | | /// <code language="csharp"> |
| | 26 | | /// public class CurrencyEditForm : Form, IValueEditorForm |
| | 27 | | /// { |
| | 28 | | /// public object EditedValue { get; set; } |
| | 29 | | /// // implement form logic here |
| | 30 | | /// } |
| | 31 | | /// |
| | 32 | | /// [Editor(typeof(ModalVisualizer<CurrencyEditForm>), typeof(UITypeEditor))] |
| | 33 | | /// public decimal Amount { get; set; } |
| | 34 | | /// </code> |
| | 35 | | /// </example> |
| | 36 | | /// <seealso cref="UITypeEditor"/> |
| | 37 | | /// <seealso cref="IValueEditorForm"/> |
| | 38 | | public class ModalVisualizer<TForm> : UITypeEditor |
| | 39 | | where TForm : Form, IValueEditorForm, new() |
| | 40 | | { |
| | 41 | | /// <inheritdoc/> |
| | 42 | | public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) |
| 8 | 43 | | => UITypeEditorEditStyle.Modal; |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Displays the modal editor form to allow the user to edit the property value. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="context">An optional type descriptor context for additional information.</param> |
| | 49 | | /// <param name="provider">A service provider for editor services.</param> |
| | 50 | | /// <param name="value">The current value of the property to edit.</param> |
| | 51 | | /// <returns> |
| | 52 | | /// The edited value returned from the modal form if the user confirms the edit; otherwise, |
| | 53 | | /// returns the original <paramref name="value"/>. |
| | 54 | | /// </returns> |
| | 55 | | public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) |
| 8 | 56 | | { |
| | 57 | | #if NET5_0_OR_GREATER |
| 8 | 58 | | using var form = new TForm(); |
| | 59 | | #else |
| | 60 | | using (var form = new TForm()) |
| | 61 | | #endif |
| 8 | 62 | | { |
| 8 | 63 | | form.EditedValue = value; // push initial value |
| 8 | 64 | | form.Context = context; // set context for the form |
| 8 | 65 | | form.Culture = CultureInfo.CurrentCulture; // set culture if available |
| 8 | 66 | | if (form.ShowEditor() == DialogResult.OK) |
| 4 | 67 | | return form.EditedValue; // get updated value |
| | 68 | |
|
| 4 | 69 | | return value; // no change |
| | 70 | | } |
| 8 | 71 | | } |
| | 72 | | } |
| | 73 | | } |