C#控件开发1—转换开关

三步绘制控件:

  1. 定义属性;
  2. 画布重绘;
  3. 添加事件;

如何自定义一个转换键(Toggle)?

在这里插入图片描述

1.主要技能:

  • 如何自定义属性;
  • 画布重绘的一般格式;
  • 控件的事件触发过程;
  • 技能扩展
    1. 转换按钮使能时添加二次确认弹框?
    2. 在From窗体中应用控件时,点击事件没有触发?
    3. 属性名称在控件属性树中的排列如何定义?
    4. 添加一个字体更改属性?

2.定义属性

  • 字体(Font)
  • 颜色(Color)
  • 字符串(String)
  • 枚举(Enum)
  • 属性说明 [Browsable(true)]
#region 属性——选中、背景颜色、文本、样式、滑块颜色

private Font displayFont = new Font("Segoe UI", 12);
[Browsable(true)]
[Category("布局_G")]
[Description("字体格式")]
public Font DisplayFont
{
   
    get {
    return displayFont; }
    set
    {
   
        if (value != null)
        {
   
            displayFont = value;
            this.Invalidate(); // Trigger redraw
        }
    }
}

private bool _checked = false;
[Browsable(true)]  //说明(需放在属性前边):是否选中
[Category("布局_G")]
[Description("是否选中")]
public bool Checked
{
   
    get {
    return _checked; }
    set
    {
   
        _checked = value;
        this.Invalidate();

        //激活触发事件
        this.MouseDown_G?.Invoke(this, null);

    }
}

private string falseText = "关闭";
[Browsable(true)]  //说明:文本关闭
[Category("布局_G")]
[Description("文本关闭")]
public string FalseText
{
   
    get {
    return falseText; }
    set {
    falseText = value; this.Invalidate(); }
}

//样式切换
public enum SwType
{
   
    Ellipse,    //椭圆
    Rectangle,  //矩形
}

private SwType switchType = SwType.Ellipse;
[Browsable(true)]  //说明:切换样式
[Category("布局_G")]
[Description("切换样式")]
public SwType SwitchType
{
   
    get {
    return switchType; }
    set {
    switchType = value; this.Invalidate(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GesLuck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值