C#Net用户自定义控件制作教程_第1页
C#Net用户自定义控件制作教程_第2页
C#Net用户自定义控件制作教程_第3页
C#Net用户自定义控件制作教程_第4页
C#Net用户自定义控件制作教程_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Net 用户自定义控件继承 UserControl 类,设计很简单的,像平时在窗体上拖控 件一样。下面跟着我一步步做:1. 建立一个工程,添加用户控件。解决方案资源管理器-CustomControl t XJ解决方案CustomControl55 (1亍项目)H生成(U) 重新生成(1) 淸理闻 发布.新逹项添枷鱼现有项.添加引用.新逹文件夹Q)添加服务引用E).Windows窗体迥查看类关丟图(V)用户控件QJ).i设肯启动项目(A)组件调试类器聖切(T)粘贴(I)X移除(V)重命名卸我项目a在Windows资源管理器中打开文件夹Q02在打开的窗体内输入控件名称,如:ucButton,按确定

2、按钮。接下来在空白 区域拖放3个.Net控件。如下图:编码代码/ / C#.Net 设计用户自定义控件/ C#制作用户自定义控件(by 易学网)/ / ToolboxBitmap(typeof(CustomControl.ucButton),ucButton.bmp) public partial class ucButton : UserControlprivate bool _IsFocused =false; /标记按钮是否为焦点状态public ucButton()InitializeComponent();this.DoHideFocusedTag(); this.MyCatpion

3、Text =this.Name;private EventHandler _OnButtonClick = null;privatestring _MyCatpionText = ucButton1;/ 按钮标题/EditorBrowsable(EditorBrowsableState.Always)Browsable(true)DefaultValue(ucButton1)public string MyCatpionTextget return _MyCatpionText; set _MyCatpionText = value; lblCaption.Text = _MyCatpionT

4、ext; /用户自定义Click事件/EditorBrowsable(EditorBrowsableState.Always)Browsable(true)public event EventHandler OnButtonClickadd _OnButtonClick += new EventHandler(value); remove _OnButtonClick -= new EventHandler(value); private void lblCaption_Click(object sender, EventArgs e) /转移 Click 事件, 触发用户自定义事件if (_

5、OnButtonClick != null) _OnButtonClick(this, e);private void lblCaption_MouseDown(object sender, MouseEventArgs e) if (_IsFocused) lblCaption.Font = new Font(lblCaption.Font.FontFamily, lblCaption.Font.Size, FontStyle.Bold);private void lblCaption_MouseUp(object sender, MouseEventArgs e) if (_IsFocus

6、ed) lblCaption.Font = new Font(lblCaption.Font.FontFamily, lblCaption.Font.Size, FontStyle.Regular);private void ucButton_SizeChanged(object sender, EventArgs e) lblUnderLine.Top = this.Height - 1; lblUnderLine.Width = this.Width - 15;/ / 还原按钮状态/ public void DoHideFocusedTag()this.pictureBox1.Image

7、= global:vjsdn.CustomControl.Properties.Resources.GrayTag;this.lblUnderLine.Visible = false; lblCaption.ForeColor = Color.Black;/ / 设计按钮为焦点状态/ public void DoShowFocusedTag()this.pictureBox1.Image global:vjsdn.CustomControl.Properties.Resources.FosedTag; this.lblUnderLine.Visible = true;lblCaption.Fo

8、reColor = Color.Blue;private void ucButton_MouseEnter(object sender, EventArgs e)if (this.Parent != null)foreach (Control c in this.Parent.Controls)if (c is ucButton) (c as ucButton).DoHideFocusedTag();this.DoShowFocusedTag();_IsFocused = true;EditorBrowsable(EditorBrowsableState.Always)Browsable(tr

9、ue)Description()public Label MyCaptionget return lblCaption; private void lblCaption_MouseEnter(object sender, EventArgs e) this.ucButton_MouseEnter(sender, e);按 F5 编译项目,建立一个测试窗体,在控件工具栏会看到有个齿轮图标的项 目。工具箱-所有指针E i ndi ngS ourEutChe匚kBoxCheekedLi etBoxC ol or D i al o gCumbuBox在窗体上拖 3 工具箱-所有指针E i ndi ng

10、S ourEutChe匚kBoxCheekedLi etBoxC ol or D i al o gCumbuBox0k指针EindingNavi gatucButtonEindingNavi gat设置按钮标题及事件。ucButtom 属性田Margin.3. S 3, 3EM ax i miiiTiS ireo. oHMinimiiiTiSi ze0, 14M o di fi ersFr i vaIEM ap t i unMyC:mtpi wlTeh t保存数据(Save Data)I1Fadding0. D 0, 0Ri ghtTuLeftNo田Si ze1T4, 14TablndexT abS tupTrueucButtom 事件MouseMoveM uus eUpMOnBu tt ordCl i ckucBut t onl_0nBut t onC V

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论