|
| 1 | +// Copyright (C) 2007 A.J.Bauer |
| 2 | +// |
| 3 | +// This software is provided as-is, without any express or implied |
| 4 | +// warranty. In no event will the authors be held liable for any damages |
| 5 | +// arising from the use of this software. |
| 6 | + |
| 7 | +// Permission is granted to anyone to use this software for any purpose, |
| 8 | +// including commercial applications, and to alter it and redistribute it |
| 9 | +// freely, subject to the following restrictions: |
| 10 | +// 1. The origin of this software must not be misrepresented; you must not |
| 11 | +// claim that you wrote the original software. if you use this software |
| 12 | +// in a product, an acknowledgment in the product documentation would be |
| 13 | +// appreciated but is not required. |
| 14 | +// 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | +// misrepresented as being the original software. |
| 16 | +// 3. This notice may not be removed or altered from any source distribution. |
| 17 | +// |
| 18 | +// ----------------------------------------------------------------------------------- |
| 19 | +// Copyright (C) 2012 Code Artist |
| 20 | +// |
| 21 | +// Added several improvement to original code created by A.J.Bauer. |
| 22 | +// Visit: http://codearteng.blogspot.com for more information on change history. |
| 23 | +// |
| 24 | +// ----------------------------------------------------------------------------------- |
| 25 | + |
| 26 | +using System.Collections; |
| 27 | +using System.ComponentModel.Design; |
| 28 | + |
| 29 | +namespace System.Windows.Forms |
| 30 | +{ |
| 31 | + public class AGaugeLabelCollection : CollectionBase |
| 32 | + { |
| 33 | + private readonly AGauge m_Owner; |
| 34 | + |
| 35 | + public AGaugeLabelCollection(AGauge sender) |
| 36 | + { |
| 37 | + m_Owner = sender; |
| 38 | + } |
| 39 | + |
| 40 | + private void NotifyChanging() |
| 41 | + { |
| 42 | + if (m_Owner != null) |
| 43 | + { |
| 44 | + m_Owner.NotifyChanging(nameof(AGauge.GaugeLabels)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private void NotifyChanged() |
| 49 | + { |
| 50 | + if (m_Owner != null) |
| 51 | + { |
| 52 | + m_Owner.NotifyChanged(nameof(AGauge.GaugeLabels)); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public AGaugeLabel this[int index] { get { return (AGaugeLabel)List[index]; } } |
| 57 | + public bool Contains(AGaugeLabel itemType) { return List.Contains(itemType); } |
| 58 | + public int Add(AGaugeLabel itemType) |
| 59 | + { |
| 60 | + itemType.SetOwner(m_Owner); |
| 61 | + if (string.IsNullOrEmpty(itemType.Name)) itemType.Name = GetUniqueName(); |
| 62 | + var ret = List.Add(itemType); |
| 63 | + if (m_Owner != null) m_Owner.RepaintControl(); |
| 64 | + return ret; |
| 65 | + } |
| 66 | + public void Remove(AGaugeLabel itemType) |
| 67 | + { |
| 68 | + List.Remove(itemType); |
| 69 | + if (m_Owner != null) m_Owner.RepaintControl(); |
| 70 | + } |
| 71 | + public void Insert(int index, AGaugeLabel itemType) |
| 72 | + { |
| 73 | + itemType.SetOwner(m_Owner); |
| 74 | + if (string.IsNullOrEmpty(itemType.Name)) itemType.Name = GetUniqueName(); |
| 75 | + List.Insert(index, itemType); |
| 76 | + if (m_Owner != null) m_Owner.RepaintControl(); |
| 77 | + } |
| 78 | + public int IndexOf(AGaugeLabel itemType) { return List.IndexOf(itemType); } |
| 79 | + public AGaugeLabel FindByName(string name) |
| 80 | + { |
| 81 | + foreach (AGaugeLabel ptrRange in List) |
| 82 | + { |
| 83 | + if (ptrRange.Name == name) return ptrRange; |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + protected override void OnInsert(int index, object value) |
| 89 | + { |
| 90 | + NotifyChanging(); |
| 91 | + var NewLabel = (AGaugeLabel)value; |
| 92 | + if (string.IsNullOrEmpty(NewLabel.Name)) |
| 93 | + { |
| 94 | + NewLabel.Name = GetUniqueName(); |
| 95 | + } |
| 96 | + NewLabel.SetOwner(m_Owner); |
| 97 | + } |
| 98 | + |
| 99 | + protected override void OnInsertComplete(int index, object value) |
| 100 | + { |
| 101 | + if (m_Owner != null) |
| 102 | + { |
| 103 | + m_Owner.RepaintControl(); |
| 104 | + } |
| 105 | + NotifyChanged(); |
| 106 | + } |
| 107 | + |
| 108 | + protected override void OnSet(int index, object oldValue, object newValue) |
| 109 | + { |
| 110 | + NotifyChanging(); |
| 111 | + } |
| 112 | + |
| 113 | + protected override void OnSetComplete(int index, object oldValue, object newValue) |
| 114 | + { |
| 115 | + m_Owner?.RepaintControl(); |
| 116 | + NotifyChanged(); |
| 117 | + } |
| 118 | + |
| 119 | + protected override void OnRemove(int index, object value) |
| 120 | + { |
| 121 | + NotifyChanging(); |
| 122 | + } |
| 123 | + |
| 124 | + protected override void OnRemoveComplete(int index, object value) |
| 125 | + { |
| 126 | + if (m_Owner != null) |
| 127 | + { |
| 128 | + m_Owner.RepaintControl(); |
| 129 | + } |
| 130 | + NotifyChanged(); |
| 131 | + } |
| 132 | + |
| 133 | + protected override void OnClear() |
| 134 | + { |
| 135 | + NotifyChanging(); |
| 136 | + } |
| 137 | + |
| 138 | + protected override void OnClearComplete() |
| 139 | + { |
| 140 | + if (m_Owner != null) |
| 141 | + { |
| 142 | + m_Owner.RepaintControl(); |
| 143 | + } |
| 144 | + NotifyChanged(); |
| 145 | + } |
| 146 | + |
| 147 | + private string GetUniqueName() |
| 148 | + { |
| 149 | + const string Prefix = "GaugeLabel"; |
| 150 | + int index = 1; |
| 151 | + while (this.Count != 0) |
| 152 | + { |
| 153 | + for (int x = 0; x < this.Count; x++) |
| 154 | + { |
| 155 | + if (this[x].Name == (Prefix + index.ToString())) |
| 156 | + continue; |
| 157 | + else |
| 158 | + return Prefix + index.ToString(); |
| 159 | + } |
| 160 | + index++; |
| 161 | + }; |
| 162 | + return Prefix + index.ToString(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments