频繁要用到的控件,在系统控件或者第三方控件又找不到合适的,就做成用户控件,节约时间,方便使用。

这里以自定义按钮控件为例,介绍用户控件创建过程。

1. 新建windows窗体控件库项目。

C#用户控件的创建


2. 创建成功后,即可看到控件的原始形态。将UserControl1类重命名为MyButton。

C#用户控件的创建


3. 这里我们要实现一个自定义的按钮,按钮普通状态下为淡蓝色,鼠标滑过时变为深蓝色,鼠标点击为深蓝色。我首先想到的方法,是使MyButton类继承Button类,并在MouseEnter和MouseLeave事件中修改按钮背景色。但这里有一个问题,Button类在鼠标点击时,会改变颜色以表示点击状态,这与我们想要的按钮不符(我们需要按钮在鼠标滑过和点击时是同样的颜色)。而Button类对按钮改变颜色的操作是在OnMouseDown事件中,若在MyButton中屏蔽该事件,则会导致Click事件不能响应,哪位朋友能知道怎么解决的话,还请赐教。因此,这里MyButton类仍然继承UserControl类。

[csharp] view plain copy
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace My_Control  
  11. {  
  12.     public partial class MyButton : UserControl  
  13.     {  
  14.         public MyButton()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.     }  
  19. }</span>  

4. 在设计器中修改MyBotton的BackColor和Size属性,使其外形如下:

C#用户控件的创建


5. 按钮上的文字,用Label控件实现。从工具箱中拖出一个Label,注意其属性的设置:Dock设置为Fill,AutoSize设置为false,TextAlign设置为MiddleCenter,Name设置为label。这样label的文字就会始终在按钮中居中。


6. 使用按钮控件时,需要设置按钮的Text属性,在这里就是label的Text属性。因此需要将label的这个属性开放出来。MyButton.cs中加入代码如下:

[csharp] view plain copy
  1. namespace My_Control  
  2. {  
  3.     public partial class MyButton : UserControl  
  4.     {  
  5.         public MyButton()  
  6.         {  
  7.             InitializeComponent();  
  8.         }  
  9.   
  10.   
  11.         [Category("外观")]  
  12.         [Description("按钮文字")]  
  13.         [DefaultValue("MyButton")]  
  14.         [Browsable(true)]  
  15.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]  
  16.         public override string Text  
  17.         {  
  18.             get  
  19.             {  
  20.                 return label.Text;  
  21.             }  
  22.             set  
  23.             {  
  24.                 label.Text = value;  
  25.             }  
  26.         }  
  27.   
  28.   
  29.     }  
  30. }  

重载了UserControl的Text属性,使其绑定到label的对应属性。注意加入的Attribute修饰:

[Category("外观")] 表示在设计器中,Text属性出现在“外观”这一分组

[Description("按钮文字")] 表示对该属性的描述文字

[Browsable(true)] 表示该属性在设计器中可见

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]这修饰很重要,表示在设计器中修改Text属性后,会自动生成设计器代码。若不加该修饰,则必须手工添加代码。

如果需要修改按钮的默认字体或者颜色,则修改MyButton的Font属性和ForeColor属性即可,注意不要修改label的这两个属性。这里讲解一下:在MyButton.Designer.cs中有这句代码:

C#用户控件的创建

也就是label作为MyButton的子控件,因此修改MyButton的Font或者ForeColor属性,就会自动对应修改label的Font和ForeColor属性。但若同时也修改了label的属性,则会优先应用label的属性设置,忽略在其父控件中的设置。

7. 添加MouseEnter和MouseLeave的鼠标事件响应,从而改变按钮的颜色,代码如下:

[csharp] view plain copy
  1. namespace My_Control  
  2. {  
  3.     public partial class MyButton : UserControl  
  4.     {  
  5.         public MyButton()  
  6.         {  
  7.             InitializeComponent();  
  8.             this.label.MouseEnter += new EventHandler(label_MouseEnter);  
  9.             this.label.MouseLeave += new EventHandler(label_MouseLeave);  
  10.         }  
  11.   
  12.         void label_MouseLeave(object sender, EventArgs e)  
  13.         {  
  14.             this.BackColor = Color.FromArgb(0, 188, 234);  
  15.         }  
  16.   
  17.         void label_MouseEnter(object sender, EventArgs e)  
  18.         {  
  19.             this.BackColor = Color.FromArgb(78, 165, 254);  
  20.         }  
  21.   
  22.         [Category("外观")]  
  23.         [Description("按钮文字")]  
  24.         [DefaultValue("MyButton")]  
  25.         [Browsable(true)]  
  26.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]  
  27.         public override string Text  
  28.         {  
  29.             get  
  30.             {  
  31.                 return label.Text;  
  32.             }  
  33.             set  
  34.             {  
  35.                 label.Text = value;  
  36.             }  
  37.         }  
  38.   
  39.   
  40.     }  
  41. }  


8. 至此,MyButton用户控件就创建完成了。按F5即可运行该控件的测试容器。

C#用户控件的创建


9. 使用MyButton:在解决方案中添加Windows Form项目,命名为TestForm。从工具箱中选择MyButton用户控件,拖入Form,即可在设计器中像设置普通按钮那样对MyButton进行设置。

C#用户控件的创建

10. 完成后,将Form程序设为启动程序,F5运行,即可查看MyButton的效果。

C#用户控件的创建



频繁要用到的控件,在系统控件或者第三方控件又找不到合适的,就做成用户控件,节约时间,方便使用。

这里以自定义按钮控件为例,介绍用户控件创建过程。

1. 新建windows窗体控件库项目。

C#用户控件的创建


2. 创建成功后,即可看到控件的原始形态。将UserControl1类重命名为MyButton。

C#用户控件的创建


3. 这里我们要实现一个自定义的按钮,按钮普通状态下为淡蓝色,鼠标滑过时变为深蓝色,鼠标点击为深蓝色。我首先想到的方法,是使MyButton类继承Button类,并在MouseEnter和MouseLeave事件中修改按钮背景色。但这里有一个问题,Button类在鼠标点击时,会改变颜色以表示点击状态,这与我们想要的按钮不符(我们需要按钮在鼠标滑过和点击时是同样的颜色)。而Button类对按钮改变颜色的操作是在OnMouseDown事件中,若在MyButton中屏蔽该事件,则会导致Click事件不能响应,哪位朋友能知道怎么解决的话,还请赐教。因此,这里MyButton类仍然继承UserControl类。

[csharp] view plain copy
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace My_Control  
  11. {  
  12.     public partial class MyButton : UserControl  
  13.     {  
  14.         public MyButton()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.     }  
  19. }</span>  

4. 在设计器中修改MyBotton的BackColor和Size属性,使其外形如下:

C#用户控件的创建


5. 按钮上的文字,用Label控件实现。从工具箱中拖出一个Label,注意其属性的设置:Dock设置为Fill,AutoSize设置为false,TextAlign设置为MiddleCenter,Name设置为label。这样label的文字就会始终在按钮中居中。


6. 使用按钮控件时,需要设置按钮的Text属性,在这里就是label的Text属性。因此需要将label的这个属性开放出来。MyButton.cs中加入代码如下:

[csharp] view plain copy
  1. namespace My_Control  
  2. {  
  3.     public partial class MyButton : UserControl  
  4.     {  
  5.         public MyButton()  
  6.         {  
  7.             InitializeComponent();  
  8.         }  
  9.   
  10.   
  11.         [Category("外观")]  
  12.         [Description("按钮文字")]  
  13.         [DefaultValue("MyButton")]  
  14.         [Browsable(true)]  
  15.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]  
  16.         public override string Text  
  17.         {  
  18.             get  
  19.             {  
  20.                 return label.Text;  
  21.             }  
  22.             set  
  23.             {  
  24.                 label.Text = value;  
  25.             }  
  26.         }  
  27.   
  28.   
  29.     }  
  30. }  

重载了UserControl的Text属性,使其绑定到label的对应属性。注意加入的Attribute修饰:

[Category("外观")] 表示在设计器中,Text属性出现在“外观”这一分组

[Description("按钮文字")] 表示对该属性的描述文字

[Browsable(true)] 表示该属性在设计器中可见

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]这修饰很重要,表示在设计器中修改Text属性后,会自动生成设计器代码。若不加该修饰,则必须手工添加代码。

如果需要修改按钮的默认字体或者颜色,则修改MyButton的Font属性和ForeColor属性即可,注意不要修改label的这两个属性。这里讲解一下:在MyButton.Designer.cs中有这句代码:

C#用户控件的创建

也就是label作为MyButton的子控件,因此修改MyButton的Font或者ForeColor属性,就会自动对应修改label的Font和ForeColor属性。但若同时也修改了label的属性,则会优先应用label的属性设置,忽略在其父控件中的设置。

7. 添加MouseEnter和MouseLeave的鼠标事件响应,从而改变按钮的颜色,代码如下:

[csharp] view plain copy
  1. namespace My_Control  
  2. {  
  3.     public partial class MyButton : UserControl  
  4.     {  
  5.         public MyButton()  
  6.         {  
  7.             InitializeComponent();  
  8.             this.label.MouseEnter += new EventHandler(label_MouseEnter);  
  9.             this.label.MouseLeave += new EventHandler(label_MouseLeave);  
  10.         }  
  11.   
  12.         void label_MouseLeave(object sender, EventArgs e)  
  13.         {  
  14.             this.BackColor = Color.FromArgb(0, 188, 234);  
  15.         }  
  16.   
  17.         void label_MouseEnter(object sender, EventArgs e)  
  18.         {  
  19.             this.BackColor = Color.FromArgb(78, 165, 254);  
  20.         }  
  21.   
  22.         [Category("外观")]  
  23.         [Description("按钮文字")]  
  24.         [DefaultValue("MyButton")]  
  25.         [Browsable(true)]  
  26.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]  
  27.         public override string Text  
  28.         {  
  29.             get  
  30.             {  
  31.                 return label.Text;  
  32.             }  
  33.             set  
  34.             {  
  35.                 label.Text = value;  
  36.             }  
  37.         }  
  38.   
  39.   
  40.     }  
  41. }  


8. 至此,MyButton用户控件就创建完成了。按F5即可运行该控件的测试容器。

C#用户控件的创建


9. 使用MyButton:在解决方案中添加Windows Form项目,命名为TestForm。从工具箱中选择MyButton用户控件,拖入Form,即可在设计器中像设置普通按钮那样对MyButton进行设置。

C#用户控件的创建

10. 完成后,将Form程序设为启动程序,F5运行,即可查看MyButton的效果。

C#用户控件的创建



频繁要用到的控件,在系统控件或者第三方控件又找不到合适的,就做成用户控件,节约时间,方便使用。

这里以自定义按钮控件为例,介绍用户控件创建过程。

1. 新建windows窗体控件库项目。

C#用户控件的创建


2. 创建成功后,即可看到控件的原始形态。将UserControl1类重命名为MyButton。

C#用户控件的创建


3. 这里我们要实现一个自定义的按钮,按钮普通状态下为淡蓝色,鼠标滑过时变为深蓝色,鼠标点击为深蓝色。我首先想到的方法,是使MyButton类继承Button类,并在MouseEnter和MouseLeave事件中修改按钮背景色。但这里有一个问题,Button类在鼠标点击时,会改变颜色以表示点击状态,这与我们想要的按钮不符(我们需要按钮在鼠标滑过和点击时是同样的颜色)。而Button类对按钮改变颜色的操作是在OnMouseDown事件中,若在MyButton中屏蔽该事件,则会导致Click事件不能响应,哪位朋友能知道怎么解决的话,还请赐教。因此,这里MyButton类仍然继承UserControl类。

[csharp] view plain copy
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace My_Control  
  11. {  
  12.     public partial class MyButton : UserControl  
  13.     {  
  14.         public MyButton()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.     }  
  19. }</span>  

4. 在设计器中修改MyBotton的BackColor和Size属性,使其外形如下:

C#用户控件的创建


5. 按钮上的文字,用Label控件实现。从工具箱中拖出一个Label,注意其属性的设置:Dock设置为Fill,AutoSize设置为false,TextAlign设置为MiddleCenter,Name设置为label。这样label的文字就会始终在按钮中居中。


6. 使用按钮控件时,需要设置按钮的Text属性,在这里就是label的Text属性。因此需要将label的这个属性开放出来。MyButton.cs中加入代码如下:

[csharp] view plain copy
  1. namespace My_Control  
  2. {  
  3.     public partial class MyButton : UserControl  
  4.     {  
  5.         public MyButton()  
  6.         {  
  7.             InitializeComponent();  
  8.         }  
  9.   
  10.   
  11.         [Category("外观")]  
  12.         [Description("按钮文字")]  
  13.         [DefaultValue("MyButton")]  
  14.         [Browsable(true)]  
  15.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]  
  16.         public override string Text  
  17.         {  
  18.             get  
  19.             {  
  20.                 return label.Text;  
  21.             }  
  22.             set  
  23.             {  
  24.                 label.Text = value;  
  25.             }  
  26.         }  
  27.   
  28.   
  29.     }  
  30. }  

重载了UserControl的Text属性,使其绑定到label的对应属性。注意加入的Attribute修饰:

[Category("外观")] 表示在设计器中,Text属性出现在“外观”这一分组

[Description("按钮文字")] 表示对该属性的描述文字

[Browsable(true)] 表示该属性在设计器中可见

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]这修饰很重要,表示在设计器中修改Text属性后,会自动生成设计器代码。若不加该修饰,则必须手工添加代码。

如果需要修改按钮的默认字体或者颜色,则修改MyButton的Font属性和ForeColor属性即可,注意不要修改label的这两个属性。这里讲解一下:在MyButton.Designer.cs中有这句代码:

C#用户控件的创建

也就是label作为MyButton的子控件,因此修改MyButton的Font或者ForeColor属性,就会自动对应修改label的Font和ForeColor属性。但若同时也修改了label的属性,则会优先应用label的属性设置,忽略在其父控件中的设置。

7. 添加MouseEnter和MouseLeave的鼠标事件响应,从而改变按钮的颜色,代码如下:

[csharp] view plain copy
  1. namespace My_Control  
  2. {  
  3.     public partial class MyButton : UserControl  
  4.     {  
  5.         public MyButton()  
  6.         {  
  7.             InitializeComponent();  
  8.             this.label.MouseEnter += new EventHandler(label_MouseEnter);  
  9.             this.label.MouseLeave += new EventHandler(label_MouseLeave);  
  10.         }  
  11.   
  12.         void label_MouseLeave(object sender, EventArgs e)  
  13.         {  
  14.             this.BackColor = Color.FromArgb(0, 188, 234);  
  15.         }  
  16.   
  17.         void label_MouseEnter(object sender, EventArgs e)  
  18.         {  
  19.             this.BackColor = Color.FromArgb(78, 165, 254);  
  20.         }  
  21.   
  22.         [Category("外观")]  
  23.         [Description("按钮文字")]  
  24.         [DefaultValue("MyButton")]  
  25.         [Browsable(true)]  
  26.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]  
  27.         public override string Text  
  28.         {  
  29.             get  
  30.             {  
  31.                 return label.Text;  
  32.             }  
  33.             set  
  34.             {  
  35.                 label.Text = value;  
  36.             }  
  37.         }  
  38.   
  39.   
  40.     }  
  41. }  


8. 至此,MyButton用户控件就创建完成了。按F5即可运行该控件的测试容器。

C#用户控件的创建


9. 使用MyButton:在解决方案中添加Windows Form项目,命名为TestForm。从工具箱中选择MyButton用户控件,拖入Form,即可在设计器中像设置普通按钮那样对MyButton进行设置。

C#用户控件的创建

10. 完成后,将Form程序设为启动程序,F5运行,即可查看MyButton的效果。

C#用户控件的创建

注意:编译路径不能含有特殊字符,例如“#”


相关文章: