|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.ComponentModel; |
| 6 | +using System.ComponentModel.Design; |
| 7 | +using System.ComponentModel.Design.Serialization; |
| 8 | + |
| 9 | +namespace System.Windows.Forms.Design |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Provides an editor for a ListView groups collection. |
| 13 | + /// </summary> |
| 14 | + internal class ListViewGroupCollectionEditor : CollectionEditor |
| 15 | + { |
| 16 | + private object _editValue; |
| 17 | + |
| 18 | + public ListViewGroupCollectionEditor(Type type) : base(type) |
| 19 | + { } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Creates a ListViewGroup instance. |
| 23 | + /// </summary> |
| 24 | + protected override object CreateInstance(Type itemType) |
| 25 | + { |
| 26 | + ListViewGroup lvg = (ListViewGroup)base.CreateInstance(itemType); |
| 27 | + |
| 28 | + // Create an unique name for the list view group. |
| 29 | + lvg.Name = CreateListViewGroupName((ListViewGroupCollection)_editValue); |
| 30 | + |
| 31 | + return lvg; |
| 32 | + } |
| 33 | + |
| 34 | + private string CreateListViewGroupName(ListViewGroupCollection lvgCollection) |
| 35 | + { |
| 36 | + string lvgName = "ListViewGroup"; |
| 37 | + string resultName; |
| 38 | + INameCreationService ncs = GetService(typeof(INameCreationService)) as INameCreationService; |
| 39 | + IContainer container = GetService(typeof(IContainer)) as IContainer; |
| 40 | + |
| 41 | + if (ncs != null && container != null) |
| 42 | + { |
| 43 | + lvgName = ncs.CreateName(container, typeof(ListViewGroup)); |
| 44 | + } |
| 45 | + |
| 46 | + // strip the digits from the end. |
| 47 | + while (char.IsDigit(lvgName[lvgName.Length - 1])) |
| 48 | + { |
| 49 | + lvgName = lvgName.Substring(0, lvgName.Length - 1); |
| 50 | + } |
| 51 | + |
| 52 | + int i = 1; |
| 53 | + resultName = lvgName + i.ToString(System.Globalization.CultureInfo.CurrentCulture); |
| 54 | + |
| 55 | + while (lvgCollection[resultName] != null) |
| 56 | + { |
| 57 | + i++; |
| 58 | + resultName = lvgName + i.ToString(System.Globalization.CultureInfo.CurrentCulture); |
| 59 | + } |
| 60 | + |
| 61 | + return resultName; |
| 62 | + } |
| 63 | + |
| 64 | + public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) |
| 65 | + { |
| 66 | + _editValue = value; |
| 67 | + object ret; |
| 68 | + |
| 69 | + // This will block while the ListViewGroupCollectionDialog is running. |
| 70 | + ret = base.EditValue(context, provider, value); |
| 71 | + |
| 72 | + // The user is done w/ the ListViewGroupCollectionDialog. |
| 73 | + // Don't need the edit value any longer |
| 74 | + _editValue = null; |
| 75 | + |
| 76 | + return ret; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments