+ UIObjectCollection:带集合个数改变事件的对象集合类

This commit is contained in:
Sunny 2021-05-19 23:06:06 +08:00
parent 4c71f2e669
commit 2c3d294a3d
8 changed files with 218 additions and 81 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -52,12 +52,21 @@ namespace Sunny.UI
/// </summary>
public event OnValueChanged ValueChanged;
public UICheckBoxGroup()
{
items.CountChange += Items_CountChange;
}
private void Items_CountChange(object sender, EventArgs e)
{
Invalidate();
}
/// <summary>
/// 析构事件
/// </summary>
~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;
}
}
}
}
}

View File

@ -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<UIRadioButton> buttons = new List<UIRadioButton>();
@ -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;
}
}
}
}
}

View File

@ -0,0 +1,188 @@
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2021 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@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();
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
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();
}
}
}