SunnyUI/SunnyUI/Controls/UIRadioButtonGroup.cs

254 lines
7.0 KiB
C#
Raw Normal View History

2020-05-11 21:11:29 +08:00
/******************************************************************************
* SunnyUI
2022-01-02 12:32:50 +08:00
* CopyRight (C) 2012-2022 ShenYongHua().
2021-02-20 15:45:47 +08:00
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
2020-05-11 21:11:29 +08:00
*
* 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.
* 使
******************************************************************************
* : UICheckBoxGroup.cs
* :
* : V3.0
2020-05-11 21:11:29 +08:00
* : 2020-01-01
*
* 2020-04-19: V2.2.3
* 2020-04-25: V2.2.4
2020-07-08 21:18:45 +08:00
* 2020-07-03: V2.2.6 ItemSize无效的Bug
2020-07-30 21:21:47 +08:00
* 2020-07-04: V2.2.6
2020-05-11 21:11:29 +08:00
******************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
namespace Sunny.UI
{
[DefaultProperty("Items")]
[DefaultEvent("ValueChanged")]
public class UIRadioButtonGroup : UIGroupBox
{
public delegate void OnValueChanged(object sender, int index, string text);
public event OnValueChanged ValueChanged;
public UIRadioButtonGroup()
{
items.CountChange += Items_CountChange;
}
private void Items_CountChange(object sender, EventArgs e)
{
Invalidate();
}
2020-05-11 21:11:29 +08:00
~UIRadioButtonGroup()
{
ClearButtons();
}
private void ClearButtons()
{
foreach (var button in buttons)
{
button.Hide();
button.Dispose();
}
buttons.Clear();
}
public void Clear()
{
Items.Clear();
ClearButtons();
SelectedIndex = -1;
Invalidate();
}
2020-05-11 21:11:29 +08:00
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
[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 UIObjectCollection Items => items;
2020-05-11 21:11:29 +08:00
private readonly UIObjectCollection items = new UIObjectCollection();
2020-05-11 21:11:29 +08:00
private void CreateBoxes()
{
2020-05-11 21:11:29 +08:00
if (Items.Count == 0) return;
if (Items.Count != buttons.Count)
{
ClearButtons();
for (int i = 0; i < Items.Count; i++)
{
UIRadioButton button = new UIRadioButton
{
BackColor = Color.Transparent,
2020-05-11 21:11:29 +08:00
Font = Font,
Parent = this,
Tag = i,
Style = Style
};
button.ValueChanged += Button_ValueChanged;
buttons.Add(button);
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
CreateBoxes();
2020-05-11 21:11:29 +08:00
int startX = StartPos.X;
int startY = TitleTop + StartPos.Y;
for (int i = 0; i < Items.Count; i++)
{
buttons[i].Text = Items[i].ToString();
int rowIndex = i / ColumnCount;
int columnIndex = i % ColumnCount;
buttons[i].Left = startX + ItemSize.Width * columnIndex + ColumnInterval * columnIndex;
buttons[i].Top = startY + ItemSize.Height * rowIndex + RowInterval * rowIndex;
buttons[i].Size = ItemSize;
2020-05-11 21:11:29 +08:00
buttons[i].Show();
}
}
private void Button_ValueChanged(object sender, bool value)
{
UIRadioButton button = (UIRadioButton)sender;
if (value)
{
SelectedIndex = button.Tag.ToString().ToInt();
}
}
private int selectedIndex = -1;
2020-05-11 21:11:29 +08:00
[Browsable(false)]
[DefaultValue(-1)]
public int SelectedIndex
{
get => selectedIndex;
2020-05-11 21:11:29 +08:00
set
{
if (buttons.Count != Items.Count)
{
CreateBoxes();
}
if (Items.Count == 0)
2020-05-11 21:11:29 +08:00
{
selectedIndex = -1;
2020-05-11 21:11:29 +08:00
return;
}
if (SelectedIndex != value)
{
if (value >= 0 && value < buttons.Count)
{
selectedIndex = value;
2020-05-11 21:11:29 +08:00
buttons[value].Checked = true;
ValueChanged?.Invoke(this, value, buttons[value].Text);
}
}
}
}
public void SelectedNone()
{
foreach (var button in buttons)
{
button.Checked = false;
}
selectedIndex = -1;
}
2020-05-11 21:11:29 +08:00
private readonly List<UIRadioButton> buttons = new List<UIRadioButton>();
private int columnCount = 1;
[DefaultValue(1)]
[Description("显示列个数"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public int ColumnCount
{
get => columnCount;
set
{
columnCount = value;
Invalidate();
}
}
private Size itemSize = new Size(150, 35);
[DefaultValue(typeof(Size), "150, 35")]
[Description("列表项大小"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public Size ItemSize
{
get => itemSize;
set
{
itemSize = value;
Invalidate();
}
}
private Point startPos = new Point(12, 12);
[DefaultValue(typeof(Point), "12, 12")]
[Description("列表项起始位置"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public Point StartPos
{
get => startPos;
set
{
startPos = value;
Invalidate();
}
}
public int columnInterval;
[DefaultValue(0)]
[Description("显示列间隔"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public int ColumnInterval
{
get => columnInterval;
set
{
columnInterval = value;
Invalidate();
}
}
private int rowInterval;
[DefaultValue(0)]
[Description("显示行间隔"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public int RowInterval
{
get => rowInterval;
set
{
rowInterval = value;
Invalidate();
}
}
}
}