diff --git a/Bin/net40/SunnyUI.Demo.exe b/Bin/net40/SunnyUI.Demo.exe index 565caebe..d70dca14 100644 Binary files a/Bin/net40/SunnyUI.Demo.exe and b/Bin/net40/SunnyUI.Demo.exe differ diff --git a/Bin/net40/SunnyUI.dll b/Bin/net40/SunnyUI.dll index fa016b5b..dcb72c6b 100644 Binary files a/Bin/net40/SunnyUI.dll and b/Bin/net40/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/SunnyUI.dll b/Bin/net5.0-windows/SunnyUI.dll index 16495c0c..a96b2a9e 100644 Binary files a/Bin/net5.0-windows/SunnyUI.dll and b/Bin/net5.0-windows/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/ref/SunnyUI.dll b/Bin/net5.0-windows/ref/SunnyUI.dll index a4673b17..380c99b2 100644 Binary files a/Bin/net5.0-windows/ref/SunnyUI.dll and b/Bin/net5.0-windows/ref/SunnyUI.dll differ diff --git a/Bin/netcoreapp3.1/SunnyUI.dll b/Bin/netcoreapp3.1/SunnyUI.dll index 78c1a89d..a34d3144 100644 Binary files a/Bin/netcoreapp3.1/SunnyUI.dll and b/Bin/netcoreapp3.1/SunnyUI.dll differ diff --git a/SunnyUI/Controls/UICheckBoxGroup.cs b/SunnyUI/Controls/UICheckBoxGroup.cs index 2ef7804a..8d5f7817 100644 --- a/SunnyUI/Controls/UICheckBoxGroup.cs +++ b/SunnyUI/Controls/UICheckBoxGroup.cs @@ -52,12 +52,21 @@ namespace Sunny.UI /// public event OnValueChanged ValueChanged; + public UICheckBoxGroup() + { + items.CountChange += Items_CountChange; + } + + private void Items_CountChange(object sender, EventArgs e) + { + Invalidate(); + } + /// /// 析构事件 /// ~UICheckBoxGroup() { - listbox?.Dispose(); ClearBoxes(); } @@ -84,23 +93,9 @@ namespace Sunny.UI [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [MergableProperty(false)] [Description("获取该多选框组中项的集合"), Category("SunnyUI")] - public ListBox.ObjectCollection Items => ListBox.Items; + public UIObjectCollection Items => items; - private CheckedListBoxEx listbox; - - private CheckedListBoxEx ListBox - { - get - { - if (listbox == null) - { - listbox = new CheckedListBoxEx(); - listbox.OnItemsCountChange += Listbox_OnItemsCountChange; - } - - return listbox; - } - } + private readonly UIObjectCollection items = new UIObjectCollection(); private void Listbox_OnItemsCountChange(object sender, EventArgs e) { @@ -353,24 +348,5 @@ namespace Sunny.UI } private bool multiChange; - - [ToolboxItem(false)] - internal class CheckedListBoxEx : CheckedListBox - { - public event EventHandler OnItemsCountChange; - - private int count = -1; - - protected override void OnDrawItem(DrawItemEventArgs e) - { - base.OnDrawItem(e); - - if (count != Items.Count) - { - OnItemsCountChange?.Invoke(this, e); - count = Items.Count; - } - } - } } } \ No newline at end of file diff --git a/SunnyUI/Controls/UIRadioButtonGroup.cs b/SunnyUI/Controls/UIRadioButtonGroup.cs index f5421ab1..5408dcd6 100644 --- a/SunnyUI/Controls/UIRadioButtonGroup.cs +++ b/SunnyUI/Controls/UIRadioButtonGroup.cs @@ -39,9 +39,18 @@ namespace Sunny.UI public event OnValueChanged ValueChanged; + public UIRadioButtonGroup() + { + items.CountChange += Items_CountChange; + } + + private void Items_CountChange(object sender, EventArgs e) + { + Invalidate(); + } + ~UIRadioButtonGroup() { - listbox?.Dispose(); ClearButtons(); } @@ -69,28 +78,9 @@ namespace Sunny.UI [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [MergableProperty(false)] [Description("列表项"), Category("SunnyUI")] - public ListBox.ObjectCollection Items => ListBox.Items; + public UIObjectCollection Items => items; - private CheckedListBoxEx listbox; - - private CheckedListBoxEx ListBox - { - get - { - if (listbox == null) - { - listbox = new CheckedListBoxEx(); - listbox.OnItemsCountChange += Listbox_OnItemsCountChange; - } - - return listbox; - } - } - - private void Listbox_OnItemsCountChange(object sender, EventArgs e) - { - Invalidate(); - } + private readonly UIObjectCollection items = new UIObjectCollection(); private void CreateBoxes() { @@ -147,11 +137,13 @@ namespace Sunny.UI } } + private int selectedIndex = -1; + [Browsable(false)] [DefaultValue(-1)] public int SelectedIndex { - get => ListBox.SelectedIndex; + get => selectedIndex; set { if (buttons.Count != Items.Count) @@ -161,7 +153,7 @@ namespace Sunny.UI if (Items.Count == 0) { - ListBox.SelectedIndex = -1; + selectedIndex = -1; return; } @@ -169,7 +161,7 @@ namespace Sunny.UI { if (value >= 0 && value < buttons.Count) { - ListBox.SelectedIndex = value; + selectedIndex = value; buttons[value].Checked = true; ValueChanged?.Invoke(this, value, buttons[value].Text); } @@ -184,7 +176,7 @@ namespace Sunny.UI button.Checked = false; } - ListBox.SelectedIndex = -1; + selectedIndex = -1; } private readonly List buttons = new List(); @@ -267,24 +259,5 @@ namespace Sunny.UI button.Font = Font; } } - - [ToolboxItem(false)] - internal class CheckedListBoxEx : CheckedListBox - { - public event EventHandler OnItemsCountChange; - - private int count = -1; - - protected override void OnDrawItem(DrawItemEventArgs e) - { - base.OnDrawItem(e); - - if (count != Items.Count) - { - OnItemsCountChange?.Invoke(this, e); - count = Items.Count; - } - } - } } } \ No newline at end of file diff --git a/SunnyUI/Units/UIObjectCollection.cs b/SunnyUI/Units/UIObjectCollection.cs new file mode 100644 index 00000000..39dfbfe1 --- /dev/null +++ b/SunnyUI/Units/UIObjectCollection.cs @@ -0,0 +1,188 @@ +/****************************************************************************** + * SunnyUI 开源控件库、工具类库、扩展类库、多页面开发框架。 + * CopyRight (C) 2012-2021 ShenYongHua(沈永华). + * QQ群:56829229 QQ:17612584 EMail:SunnyUI@QQ.Com + * + * Blog: https://www.cnblogs.com/yhuse + * Gitee: https://gitee.com/yhuse/SunnyUI + * GitHub: https://github.com/yhuse/SunnyUI + * + * SunnyUI.dll can be used for free under the GPL-3.0 license. + * If you use this code, please keep this note. + * 如果您使用此代码,请保留此说明。 + ****************************************************************************** + * 文件名称: UIObjectCollection.cs + * 文件说明: 带集合个数改变事件的对象集合类 + * 当前版本: V3.0 + * 创建日期: 2021-05-19 + * + * 2021-05-19: V3.0.3 增加文件说明 +******************************************************************************/ + +using System; +using System.Collections; + +namespace Sunny.UI +{ + [Serializable] + public class UIObjectCollection : IList + { + private ArrayList data = new ArrayList(); + + public event EventHandler CountChange; + + public object this[int index] + { + get => data[index]; + set => data[index] = value; + } + + public int Count => data.Count; + + bool IList.IsReadOnly => false; + + bool IList.IsFixedSize => false; + + public int Add(object value) + { + int count = data.Add(value); + CountChange?.Invoke(this, new EventArgs()); + return count; + } + + public void AddRange(object[] value) + { + if (value == null) + { + throw new ArgumentNullException("value is null"); + } + + data.AddRange(value); + CountChange?.Invoke(this, new EventArgs()); + } + + public void Clear() + { + data.Clear(); + CountChange?.Invoke(this, new EventArgs()); + } + + public bool Contains(object value) + { + return data.Contains(value); + } + + public void CopyTo(object[] array, int index) + { + data.CopyTo(array, index); + } + + public UIObjectEnumerator GetEnumerator() + { + return new UIObjectEnumerator(this); + } + + public int IndexOf(object value) + { + return data.IndexOf(value); + } + + public void Insert(int index, object value) + { + data.Insert(index, value); + CountChange?.Invoke(this, new EventArgs()); + } + + public bool IsReadOnly => false; + + public bool IsSynchronized => false; + + public void Remove(object value) + { + data.Remove(value); + CountChange?.Invoke(this, new EventArgs()); + } + + public void RemoveAt(int index) + { + data.RemoveAt(index); + CountChange?.Invoke(this, new EventArgs()); + } + + public object SyncRoot => data.SyncRoot; + + object IList.this[int index] + { + get => this[index]; + set => this[index] = value; + } + + int IList.Add(object value) + { + int count = Add(value); + CountChange?.Invoke(this, new EventArgs()); + return count; + } + + bool IList.Contains(object value) + { + return Contains(value); + } + + + int IList.IndexOf(object value) + { + return IndexOf(value); + } + + void IList.Insert(int index, object value) + { + Insert(index, value); + CountChange?.Invoke(this, new EventArgs()); + } + + void IList.Remove(object value) + { + Remove(value); + CountChange?.Invoke(this, new EventArgs()); + } + + void ICollection.CopyTo(Array array, int index) + { + data.CopyTo(array, index); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return data.GetEnumerator(); + } + } + + /// + /// [To be supplied.] + /// + public class UIObjectEnumerator + { + private readonly IEnumerator baseEnumerator; + private readonly IEnumerable temp; + + internal UIObjectEnumerator(UIObjectCollection mappings) + { + this.temp = (IEnumerable)(mappings); + this.baseEnumerator = temp.GetEnumerator(); + } + + public object Current => baseEnumerator.Current; + + public bool MoveNext() + { + return baseEnumerator.MoveNext(); + } + + public void Reset() + { + baseEnumerator.Reset(); + } + + } +} \ No newline at end of file