第三章例题:为控件添加设计时支持

本文介绍了一个使用C#实现的Windows窗体控件(UserControl)的自定义属性设置方法,通过实例展示了如何为按钮(button1)设置背景颜色,并提供了控件类的源代码。
对教材3.3部分进行演示

 1 None.gif [ToolboxBitmap( @" E:\教学\基于C#的Windows应用程序设计\第三章\test_cra\App.ico " )]
 2 None.gif public   class  UserControl1 : System.Windows.Forms.UserControl
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 4InBlock.gif    private System.Windows.Forms.Button button1;
 5InBlock.gif    private System.Windows.Forms.Button button2;
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// 必需的设计器变量。
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    private System.ComponentModel.Container components = null;
10InBlock.gif
11InBlock.gif    public UserControl1()
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        // 该调用是 Windows.Forms 窗体设计器所必需的。
14InBlock.gif        InitializeComponent();
15InBlock.gif
16InBlock.gif        // TODO: 在 InitComponent 调用后添加任何初始化
17InBlock.gif
18ExpandedSubBlockEnd.gif    }

19InBlock.gif
20ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
21InBlock.gif    /// 清理所有正在使用的资源。
22ExpandedSubBlockEnd.gif    /// </summary>

23InBlock.gif    protected override void Dispose( bool disposing )
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25InBlock.gif        if( disposing )
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            if( components != null )
28InBlock.gif                components.Dispose();
29ExpandedSubBlockEnd.gif        }

30InBlock.gif        base.Dispose( disposing );
31ExpandedSubBlockEnd.gif    }

32InBlock.gif
33InBlock.gif    [Browsable(true),Description("自定义的属性描述"),Category("自定义的分类")]
34InBlock.gif    public Color Button1Color
35ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
36InBlock.gif        set
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            button1.BackColor = value;
39ExpandedSubBlockEnd.gif        }

40InBlock.gif        get
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            return button1.BackColor;
43ExpandedSubBlockEnd.gif        }

44ExpandedSubBlockEnd.gif    }

45ExpandedBlockEnd.gif}

完整源代码下载: control_runtime_attribute.rar

转载于:https://www.cnblogs.com/hhdn/archive/2007/04/15/714243.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值