溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C# 中怎么利用Winform自定義一個(gè)單選框

發(fā)布時(shí)間:2021-07-07 15:14:38 來源:億速云 閱讀:533 作者:Leah 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關(guān)C# 中怎么利用Winform自定義一個(gè)單選框,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

添加一個(gè)用戶控件,命名為:UCRadioButton

看一下有哪些屬性

[Description("選中改變事件"), Category("自定義")]
public event EventHandler CheckedChangeEvent;

private Font _Font = new Font("微軟雅黑", 12);
[Description("字體"), Category("自定義")]
public new Font Font
{
    get { return _Font; }
    set
    {
        _Font = value;
        label1.Font = value;
    }
}

private Color _ForeColor = Color.FromArgb(62, 62, 62);
[Description("字體顏色"), Category("自定義")]
public new Color ForeColor
{
    get { return _ForeColor; }
    set
    {
        label1.ForeColor = value;
        _ForeColor = value;
    }
}
private string _Text = "單選按鈕";
[Description("文本"), Category("自定義")]
public string TextValue
{
    get { return _Text; }
    set
    {
        label1.Text = value;
        _Text = value;
    }
}
private bool _checked = 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);
            }
        }
    }
}

private string _groupName;

[Description("分組名稱"), Category("自定義")]
public string GroupName
{
    get { return _groupName; }
    set { _groupName = value; }
}

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;
            }
        }
    }
}
 

當(dāng)選中狀態(tài)改變時(shí)需要根據(jù)分組名稱來做相應(yīng)的處理

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;
                }
            }
        }
    }
}
 

當(dāng)點(diǎn)擊時(shí)改變選中狀態(tài)

private void Radio_MouseDown(object sender, MouseEventArgs e)
{
    this.Checked = true;
}
 

加載時(shí)做一下處理,防止多選了

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;
                }
            }
        }
    }
}
 

來看下完整的代碼吧

// 版權(quán)所有  黃正輝  交流群:568015492   QQ:623128629
// 文件名稱:UCRadioButton.cs
// 創(chuàng)建日期:2019-08-15 16:03:13
// 功能描述:RadioButton
// 項(xiàng)目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
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
{
    [DefaultEvent("CheckedChangeEvent")]
    public partial class UCRadioButton : UserControl
    {
        [Description("選中改變事件"), Category("自定義")]
        public event EventHandler CheckedChangeEvent;

        private Font _Font = new Font("微軟雅黑", 12);
        [Description("字體"), Category("自定義")]
        public new Font Font
        {
            get { return _Font; }
            set
            {
                _Font = value;
                label1.Font = value;
            }
        }

        private Color _ForeColor = Color.FromArgb(62, 62, 62);
        [Description("字體顏色"), Category("自定義")]
        public new Color ForeColor
        {
            get { return _ForeColor; }
            set
            {
                label1.ForeColor = value;
                _ForeColor = value;
            }
        }
        private string _Text = "單選按鈕";
        [Description("文本"), Category("自定義")]
        public string TextValue
        {
            get { return _Text; }
            set
            {
                label1.Text = value;
                _Text = value;
            }
        }
        private bool _checked = 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);
                    }
                }
            }
        }

        private string _groupName;

        [Description("分組名稱"), Category("自定義")]
        public string GroupName
        {
            get { return _groupName; }
            set { _groupName = value; }
        }

        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;
                    }
                }
            }
        }
        public UCRadioButton()
        {
            InitializeComponent();
        }

        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;
                        }
                    }
                }
            }
        }

        private void Radio_MouseDown(object sender, MouseEventArgs e)
        {
            this.Checked = true;
        }

        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;
                        }
                    }
                }
            }
        }
    }
}
 
namespace HZH_Controls.Controls
{
    partial class UCRadioButton
    {
        /// <summary> 
        /// 必需的設(shè)計(jì)器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 組件設(shè)計(jì)器生成的代碼

        /// <summary> 
        /// 設(shè)計(jì)器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內(nèi)容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Font = new System.Drawing.Font("微軟雅黑", 12F);
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((int)(((byte)(62)))));
            this.label1.Location = new System.Drawing.Point(18, 0);
            this.label1.Name = "label1";
            this.label1.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.label1.Size = new System.Drawing.Size(215, 30);
            this.label1.TabIndex = 3;
            this.label1.Text = "單選按鈕";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
            // 
            // panel1
            // 
            this.panel1.BackgroundImage = global::HZH_Controls.Properties.Resources.radioButton0;
            this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(18, 30);
            this.panel1.TabIndex = 2;
            this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
            // 
            // UCRadioButton
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Controls.Add(this.label1);
            this.Controls.Add(this.panel1);
            this.Name = "UCRadioButton";
            this.Size = new System.Drawing.Size(233, 30);
            this.Load += new System.EventHandler(this.UCRadioButton_Load);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Panel panel1;
    }
}

以上就是C# 中怎么利用Winform自定義一個(gè)單選框,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI