热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

(六)C#Winform自定义控件单选框

六,c,winform,自定

准备工作

准备4个图片,分别对应选中,没选中,选中禁用,没选中禁用

单选框需要支持分组,当同一面板上具有多种选择的时候,分组就显得更为重要了

开始

添加一个用户控件,命名为: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(626262);
[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(thisnull);
            }
        }
    }
}

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

当选中状态改变时需要根据分组名称来做相应的处理

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

来看下完整的代码吧

// 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCRadioButton.cs
// 创建日期:2019-08-15 16:03:13
// 功能描述:RadioButton
// 项目地址: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(626262);
        [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(thisnull);
                    }
                }
            }
        }

        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
    {
        ///  
        /// 必需的设计器变量。
        /// 

        private System.ComponentModel.IContainer components = null;

        ///  
        /// 清理所有正在使用的资源。
        /// 

        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        ///  
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// 

        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(180);
            this.label1.Name = "label1";
            this.label1.Padding = new System.Windows.Forms.Padding(5000);
            this.label1.Size = new System.Drawing.Size(21530);
            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(00);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(1830);
            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(23330);
            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;
    }
}

用处及效果

用途: 就是单选框

效果:

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧,另本站转载地址:https://dotnet9.com/5288.html。

  • 作者:冰封一夏
  • 出处:http://www.hzhcontrols.com/doc.html
  • 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • GitHub:https://github.com/kwwwvagaa/NetWinformControl
  • 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

Dotnet9网站常驻编辑


 

长按关注我,

欢迎技术交流!

-好东西要转发,设为"星标"★抢先看-

点击阅读原文,查看HZHControls站点更多博文。

本文分享自微信公众号 - Dotnet9(dotnet9_com)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 有没有一种方法可以在不继承UIAlertController的子类或不涉及UIAlertActions的情况下 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
author-avatar
手机用户2602922383_687
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有