// *********************************************************************** // Assembly : HZH_Controls // Created : 08-08-2019 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace HZH_Controls.Controls { /// /// Class UCRadioButton. /// Implements the /// /// [DefaultEvent("CheckedChangeEvent")] public partial class UCRadioButton : UserControl { /// /// Occurs when [checked change event]. /// [Description("选中改变事件"), Category("自定义")] public event EventHandler CheckedChangeEvent; /// /// The font /// private Font _Font = new Font("微软雅黑", 12); /// /// 获取或设置控件显示的文字的字体。 /// /// The font. /// /// /// /// /// /// [Description("字体"), Category("自定义")] public new Font Font { get { return _Font; } set { _Font = value; label1.Font = value; } } /// /// The fore color /// private Color _ForeColor = Color.FromArgb(62, 62, 62); /// /// 获取或设置控件的前景色。 /// /// The color of the fore. /// /// /// [Description("字体颜色"), Category("自定义")] public new Color ForeColor { get { return _ForeColor; } set { label1.ForeColor = value; _ForeColor = value; } } /// /// The text /// private string _Text = "单选按钮"; /// /// Gets or sets the text value. /// /// The text value. [Description("文本"), Category("自定义")] public string TextValue { get { return _Text; } set { label1.Text = value; _Text = value; } } /// /// The checked /// private bool _checked = false; /// /// Gets or sets a value indicating whether this is checked. /// /// true if checked; otherwise, false. [Description("是否选中"), Category("自定义")] public bool Checked { get { return _checked; } set { if (_checked != value) { _checked = value; if (base.Enabled) { if (_checked) { panel1.BackgroundImage = Properties.Resources.radioButton1; } else { panel1.BackgroundImage = Properties.Resources.radioButton0; } } else { if (_checked) { panel1.BackgroundImage = Properties.Resources.radioButton10; } else { panel1.BackgroundImage = Properties.Resources.radioButton00; } } SetCheck(value); if (CheckedChangeEvent != null) { CheckedChangeEvent(this, null); } } } } /// /// The group name /// private string _groupName; /// /// Gets or sets the name of the group. /// /// The name of the group. [Description("分组名称"), Category("自定义")] public string GroupName { get { return _groupName; } set { _groupName = value; } } /// /// 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。 /// /// true if enabled; otherwise, false. /// /// /// /// /// /// public new bool Enabled { get { return base.Enabled; } set { base.Enabled = value; if (value) { if (_checked) { panel1.BackgroundImage = Properties.Resources.radioButton1; } else { panel1.BackgroundImage = Properties.Resources.radioButton0; } } else { if (_checked) { panel1.BackgroundImage = Properties.Resources.radioButton10; } else { panel1.BackgroundImage = Properties.Resources.radioButton00; } } } } /// /// Initializes a new instance of the class. /// public UCRadioButton() { InitializeComponent(); } /// /// Sets the check. /// /// if set to true [BLN]. private void SetCheck(bool bln) { if (!bln) return; if (this.Parent != null) { foreach (Control c in this.Parent.Controls) { if (c is UCRadioButton && c != this) { UCRadioButton uc = (UCRadioButton)c; if (_groupName == uc.GroupName && uc.Checked) { uc.Checked = false; return; } } } } } /// /// Handles the MouseDown event of the Radio control. /// /// The source of the event. /// The instance containing the event data. private void Radio_MouseDown(object sender, MouseEventArgs e) { this.Checked = true; } /// /// Handles the Load event of the UCRadioButton control. /// /// The source of the event. /// The instance containing the event data. private void UCRadioButton_Load(object sender, EventArgs e) { if (this.Parent != null && this._checked) { foreach (Control c in this.Parent.Controls) { if (c is UCRadioButton && c != this) { UCRadioButton uc = (UCRadioButton)c; if (_groupName == uc.GroupName && uc.Checked) { Checked = false; return; } } } } } } }