单独一个ComboBox实现不了嘛?
解决方案 »
- 自动设置宽度datagridview
- 抽象类和接口能实例化吗?能定义抽象类数组和接口数组吗?
- 如何 在类外面创建委托
- 求助gridview中textbox的问题
- DataGridView显示数据的格式设置问题
- 求救:已经搜索整个csdn,没有结果,望高人们指点:在C#下怎样对QQ的ip地址数据库文件qqwry.dat文件进行解析和查询?
- c# 如何获得存储过程返回值,帮我看看哪里不对。谢谢
- VS2003的一个BUG
- 学生发帖求指教用BinaryReader和BinaryWriter读写二进制文件问题
- 谁能讲解一下,如何把这个例程拿到自己的程序中来实现xp效果!!!
- DataFrid: ture 不是 Boolean 的有效值???为何?
- 在.net中的事务处理一般用在什么地方?
vs.net里有自定义控件的项目类别,你试试
csc /t:library /out:name.dll name.cs
2)成控件在Visual Studio .net 中, 用Windows控件库来作为模板,生成一个新的项目.项目的名称为 ctlCuteButton.
首先么做的就是要修改cuteButton的父类
修改下面这行:public class cuteButton : System.Windows.Forms.Control改为:public class cuteButton : System.Windows.Forms.Button现在我们的控件就是从System.Windows.Forms.Button 类来继承了.现在让我们给这个控件加一些属性,在cuteButton类中加入下面的代码就可以完成.
private Color m_color1 = Color.LightGreen; //第一种颜色private Color m_color2 = Color.DarkBlue; // 第二种颜色private int m_color1Transparent = 64; // 第一种颜色的透明度 private int m_color2Transparent = 64; // 第二种颜色的透明度 { get { return m_color1; } set { m_color1 = value; Invalidate(); }} public Color cuteColor2{ get { return m_color2; } set { m_color2 = value; Invalidate(); }} public int cuteTransparent1{ get { return m_color1Transparent; } set { m_color1Transparent = value; Invalidate(); }} public int cuteTransparent2{ get { return m_color2Transparent; } set { m_color2Transparent = value; Invalidate(); }}上面的 Invalidate() 方法是用来在设计和使用时用来刷新控件的内部.重写父类的Paint 事件
// Calling the base class OnPaintbase.OnPaint(pe);// Create two semi-transparent colorsColor c1 = Color.FromArgb(m_color1Transparent , m_color1);Color c2 = Color.FromArgb(m_color2Transparent , m_color2);Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10);pe.Graphics.FillRectangle (b, ClientRectangle);b.Dispose();现在我们可以编译这控件了,可以使用快捷键<Ctrl>+<Shift>+<B>.
下面是全部的代码:using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms; namespace ctlCuteButton{ /// <summary> /// Summary description for cuteButton. /// </summary> public class cuteButton : System.Windows.Forms.Button { private Color m_color1 = Color.LightGreen; //第一种颜色 private Color m_color2 = Color.DarkBlue; // 第二种颜色 private int m_color1Transparent = 64; // 第一种颜色的透明度 private int m_color2Transparent = 64; // 第二种颜色的透明度 public Color cuteColor1 { get { return m_color1; } set { m_color1 = value; Invalidate(); } } public Color cuteColor2 { get { return m_color2; } set { m_color2 = value; Invalidate(); } } public int cuteTransparent1 { get { return m_color1Transparent; } set { m_color1Transparent = value; Invalidate(); } } public int cuteTransparent2 { get { return m_color2Transparent; } set { m_color2Transparent = value; Invalidate(); } } public cuteButton() { } protected override void OnPaint(PaintEventArgs pe) { // Calling the base class OnPaint base.OnPaint(pe); // Create two semi-transparent colors Color c1 = Color.FromArgb (m_color1Transparent , m_color1); Color c2 = Color.FromArgb (m_color2Transparent , m_color2); Brush b = new System.Drawing.Drawing2D.LinearGradientBrush (ClientRectangle, c1, c2, 10); pe.Graphics.FillRectangle (b, ClientRectangle); b.Dispose(); }}}如何来使用你的控件打开VS.Net,用Winows应用程序模板来产生一个新的项目.
在新的项目中,你可以把上面编译过的控件添加到工具箱中. 在工具箱中,右键,定制工具箱,然后添加.Net控件,然后浏览,选中控件的dll文件(在我们的例子中,这个文件是ctlCuteButton\bin\debug\cuteButton.dll).这是cuteButton控件就会出现在工具箱中.
你可以尝试改变控件的一些属性,比如 cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2.现在,你就完全知道如果开发和使用自定义控件了吧.祝你好运.