您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 机械/模具设计 > CNet用户自定义控件制作教程
Net用户自定义控件继承UserControl类,设计很简单的,像平时在窗体上拖控件一样。下面跟着我一步步做:1.建立一个工程,添加用户控件。2.在打开的窗体内输入控件名称,如:ucButton,按确定按钮。接下来在空白区域拖放3个.Net控件。如下图:3.编码代码///summary///C#.Net设计用户自定义控件///C#制作用户自定义控件(by易学网)////summary////summary[ToolboxBitmap(typeof(CustomControl.ucButton),ucButton.bmp)]publicpartialclassucButton:UserControl{privatebool_IsFocused=false;//标记按钮是否为焦点状态publicucButton(){InitializeComponent();this.DoHideFocusedTag();this.MyCatpionText=this.Name;}privateEventHandler_OnButtonClick=null;privatestring_MyCatpionText=ucButton1;///summary///按钮标题////summary[EditorBrowsable(EditorBrowsableState.Always)][Browsable(true)][DefaultValue(ucButton1)]publicstringMyCatpionText{get{return_MyCatpionText;}set{_MyCatpionText=value;lblCaption.Text=_MyCatpionText;}}///summary///用户自定义Click事件////summary[EditorBrowsable(EditorBrowsableState.Always)][Browsable(true)]publiceventEventHandlerOnButtonClick{add{_OnButtonClick+=newEventHandler(value);}remove{_OnButtonClick-=newEventHandler(value);}}privatevoidlblCaption_Click(objectsender,EventArgse){//转移Click事件,触发用户自定义事件if(_OnButtonClick!=null)_OnButtonClick(this,e);}privatevoidlblCaption_MouseDown(objectsender,MouseEventArgse){if(_IsFocused){lblCaption.Font=newFont(lblCaption.Font.FontFamily,lblCaption.Font.Size,FontStyle.Bold);}}privatevoidlblCaption_MouseUp(objectsender,MouseEventArgse){if(_IsFocused){lblCaption.Font=newFont(lblCaption.Font.FontFamily,lblCaption.Font.Size,FontStyle.Regular);}}privatevoiducButton_SizeChanged(objectsender,EventArgse){lblUnderLine.Top=this.Height-1;lblUnderLine.Width=this.Width-15;}///summary///还原按钮状态////summarypublicvoidDoHideFocusedTag(){this.pictureBox1.Image=global::vjsdn.CustomControl.Properties.Resources.GrayTag;this.lblUnderLine.Visible=false;lblCaption.ForeColor=Color.Black;}///summary///设计按钮为焦点状态////summarypublicvoidDoShowFocusedTag(){this.pictureBox1.Image=global::vjsdn.CustomControl.Properties.Resources.FosedTag;this.lblUnderLine.Visible=true;lblCaption.ForeColor=Color.Blue;}privatevoiducButton_MouseEnter(objectsender,EventArgse){if(this.Parent!=null){foreach(Controlcinthis.Parent.Controls){if(cisucButton)(casucButton).DoHideFocusedTag();}}this.DoShowFocusedTag();_IsFocused=true;}[EditorBrowsable(EditorBrowsableState.Always)][Browsable(true)][Description()]publicLabelMyCaption{get{returnlblCaption;}}privatevoidlblCaption_MouseEnter(objectsender,EventArgse){this.ucButton_MouseEnter(sender,e);}}4.按F5编译项目,建立一个测试窗体,在控件工具栏会看到有个齿轮图标的项目。在窗体上拖3个ucButton。5.设置按钮标题及事件。6.运行程序
本文标题:CNet用户自定义控件制作教程
链接地址:https://www.777doc.com/doc-2906494 .html