自定义控件之选择框CheckerWithChild

本文详细介绍了如何在C#中自定义控件类CheckerChild,包括其内部逻辑和事件处理,并展示了如何在一个Form中使用这个自定义控件。通过实例演示了控件的创建、布局和交互特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义控件类CheckerChild,继承自UserControl类:

using System;
using System.Windows.Forms;
using System.Drawing;

namespace CsDev
{
    //自定义自控件
    class CheckerChild:UserControl
    {
        bool bChecked = false;
        public CheckerChild()
        {
            ResizeRedraw = true;
        }
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            bChecked = !bChecked;
            Invalidate();
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch(e.KeyCode)
            {
                case Keys.Enter:
                case Keys.Space:
                    OnClick(new EventArgs());
                    break;
            }
        }
        protected override void OnGotFocus(EventArgs e)//获得焦点
        {
            Invalidate();
            base.OnGotFocus(e);
        }
        protected override void OnLostFocus(EventArgs e)//失去焦点
        {
            Invalidate();
            base.OnGotFocus(e);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics grph = e.Graphics;
            Pen pen = new Pen(Color.Red);

            grph.DrawRectangle(pen, ClientRectangle);
            if(bChecked)//画X
            {
                grph.DrawLine(pen,0,0,ClientSize.Width,ClientSize.Height);
                grph.DrawLine(pen,0,ClientSize.Height,ClientSize.Width,0);
            }

            if (Focused)//以前景色来清除X
            {
                grph.DrawRectangle(new Pen(ForeColor,5),ClientRectangle);
            }
            base.OnPaint(e);
        }
    }
}

使用自定义控件:

using System;
using System.Windows.Forms;
using System.Drawing;

namespace CsDev
{
    class CheckerWithChild:Form
    {
        const int xNum = 5;
        const int yNum = 4;
        CheckerChild[,] acntlChild;//定义自定义控件变量
        
        static void Main()
        {
            Application.Run(new CheckerWithChild());
        }
        public CheckerWithChild()
        {
            Text = "CheckerWithChild";
            CreateChildren();
            OnResize(EventArgs.Empty);
        }
        protected virtual void CreateChildren()
        {
            acntlChild=new CheckerChild[yNum,xNum];
            //添加自定义控件
            for(int y=0;y<yNum;y++)
                for (int x = 0; x <xNum; x++)
                {
                    acntlChild[y, x] = new CheckerChild();
                    acntlChild[y, x].Parent = this;
                }
        }
        protected override void OnResize(EventArgs e)
        {
            int cxBlock = ClientSize.Width / xNum;
            int cyBlock = ClientSize.Height / yNum;

            for (int y = 0; y < yNum; y++)
                for (int x = 0; x < xNum; x++)
                {
                    acntlChild[y, x].Location = new Point(x*cxBlock,y*cyBlock);
                    acntlChild[y, x].Size = new Size(cxBlock,cyBlock);
                }

        }
    }
}

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值