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 QQ:17612584 EMail:SunnyUI@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
|
|
|
|
|
* 文件说明: 单选框组
|
2021-01-05 16:43:02 +08:00
|
|
|
|
* 当前版本: 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;
|
|
|
|
|
|
2021-05-19 23:06:06 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 17:03:35 +08:00
|
|
|
|
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)]
|
2020-08-09 15:06:09 +08:00
|
|
|
|
[Description("列表项"), Category("SunnyUI")]
|
2021-05-19 23:06:06 +08:00
|
|
|
|
public UIObjectCollection Items => items;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2021-05-19 23:06:06 +08:00
|
|
|
|
private readonly UIObjectCollection items = new UIObjectCollection();
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2020-07-04 22:27:04 +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
|
|
|
|
|
{
|
2020-07-03 21:40:58 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-04 22:27:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2020-07-03 21:40:58 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 23:06:06 +08:00
|
|
|
|
private int selectedIndex = -1;
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
[Browsable(false)]
|
|
|
|
|
[DefaultValue(-1)]
|
|
|
|
|
public int SelectedIndex
|
|
|
|
|
{
|
2021-05-19 23:06:06 +08:00
|
|
|
|
get => selectedIndex;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2020-07-04 22:27:04 +08:00
|
|
|
|
if (buttons.Count != Items.Count)
|
|
|
|
|
{
|
|
|
|
|
CreateBoxes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Items.Count == 0)
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
2021-05-19 23:06:06 +08:00
|
|
|
|
selectedIndex = -1;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SelectedIndex != value)
|
|
|
|
|
{
|
|
|
|
|
if (value >= 0 && value < buttons.Count)
|
|
|
|
|
{
|
2021-05-19 23:06:06 +08:00
|
|
|
|
selectedIndex = value;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
buttons[value].Checked = true;
|
|
|
|
|
ValueChanged?.Invoke(this, value, buttons[value].Text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 12:39:06 +08:00
|
|
|
|
public void SelectedNone()
|
|
|
|
|
{
|
|
|
|
|
foreach (var button in buttons)
|
|
|
|
|
{
|
|
|
|
|
button.Checked = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 23:06:06 +08:00
|
|
|
|
selectedIndex = -1;
|
2020-10-17 12:39:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
private readonly List<UIRadioButton> buttons = new List<UIRadioButton>();
|
|
|
|
|
|
|
|
|
|
private int columnCount = 1;
|
|
|
|
|
|
|
|
|
|
[DefaultValue(1)]
|
2020-08-09 15:06:09 +08:00
|
|
|
|
[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")]
|
2020-08-09 15:06:09 +08:00
|
|
|
|
[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")]
|
2020-08-09 15:06:09 +08:00
|
|
|
|
[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)]
|
2020-08-09 15:06:09 +08:00
|
|
|
|
[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)]
|
2020-08-09 15:06:09 +08:00
|
|
|
|
[Description("显示行间隔"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public int RowInterval
|
|
|
|
|
{
|
|
|
|
|
get => rowInterval;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
rowInterval = value;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|