C#.Net用户自定义控件制作教程[vjsdn]
2010-07-23 12:41
337 查看
Net用户自定义控件继承UserControl类,设计很简单的,像平时在窗体上拖控件一样。
下面跟着我一步步做:
1. 建立一个工程,添加用户控件。
代码
4. 按F5编译项目,建立一个测试窗体,在控件工具栏会看到有个齿轮图标的项目。
在窗体上拖3个ucButton。
5.设置按钮标题及事件。
6.运行程序
下面跟着我一步步做:
1. 建立一个工程,添加用户控件。
代码
/// <summary> /// C#.Net 设计用户自定义控件 /// C#制作用户自定义控件 (by www.vjsdn.net 易学网) /// </summary> /// </summary> [ToolboxBitmap(typeof(CustomControl.ucButton), "ucButton.bmp")] public partial class ucButton : UserControl { private bool _IsFocused = false; //标记按钮是否为焦点状态 public ucButton() { InitializeComponent(); this.DoHideFocusedTag(); this.MyCatpionText = this.Name; } private EventHandler _OnButtonClick = null; private string _MyCatpionText = "ucButton1"; /// <summary> /// 按钮标题 /// </summary> [EditorBrowsable(EditorBrowsableState.Always)] [Browsable(true)] [DefaultValue("ucButton1")] public string MyCatpionText { get { return _MyCatpionText; } set { _MyCatpionText = value; lblCaption.Text = _MyCatpionText; } } /// <summary> /// 用户自定义Click事件 /// </summary> [EditorBrowsable(EditorBrowsableState.Always)] [Browsable(true)] public event EventHandler OnButtonClick { add { _OnButtonClick += new EventHandler(value); } remove { _OnButtonClick -= new EventHandler(value); } } private void lblCaption_Click(object sender, EventArgs e) { //转移Click事件, 触发用户自定义事件 if (_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 (_IsFocused) { 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; } /// <summary> /// 还原按钮状态 /// </summary> public void DoHideFocusedTag() { this.pictureBox1.Image = global::vjsdn.CustomControl.Properties.Resources.GrayTag; this.lblUnderLine.Visible = false; lblCaption.ForeColor = Color.Black; } /// <summary> /// 设计按钮为焦点状态 /// </summary> public void DoShowFocusedTag() { this.pictureBox1.Image = global::vjsdn.CustomControl.Properties.Resources.FosedTag; this.lblUnderLine.Visible = true; lblCaption.ForeColor = 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(true)] [Description("")] public Label MyCaption { get { return lblCaption; } } private void lblCaption_MouseEnter(object sender, EventArgs e) { this.ucButton_MouseEnter(sender, e); } }
4. 按F5编译项目,建立一个测试窗体,在控件工具栏会看到有个齿轮图标的项目。
在窗体上拖3个ucButton。
5.设置按钮标题及事件。
6.运行程序
相关文章推荐
- C#.Net用户自定义控件制作教程
- C# 用户自定义控件制作教程
- C#.net用户自定义控件。
- 如何在ASP.NET中制作Web用户自定义控件,并在aspx页面中使用它的方法和属性?
- [VB.NET]开发用户自定义控件
- 将ASP.NET用户控件转化为自定义控件
- Asp.net动态加载用户自定义控件,并转换成HTML代码[转]
- JS图表控件FusionCharts使用教程:自定义图表的工具提示
- ASP.NET用户控件事件的定义和实践--C#里的委托和事件实现Observer
- ASP.NET用户控件事件的定义和实践-- 自定义事件数据类
- 【C#.NET】自定义“验证码”控件(转)
- 基于地图开发控件GMap.Net 使用 (五) 原创 用户自定义标签图片
- asp.net中给用户控件添加自定义事件
- .NET中WebBrowser控件内部页面的JS代码与外部C#代码的相互调用
- ASP.NET(C#)中给控件添加客户端js事件
- C# 自定义用户控件
- C#WinForm制作异形窗体/控件-.NET教程,C#语言
- ASP.NET自定义用户控件
- C#.NET 封装自定义组件(控件)Dll
- ASP.Net访问母版页(MasterPage)控件、属性、方法及母版页中调用内容页,获取用户自定义控件里面的子控件的方法