相关概念
1. 组件
组件是指可重复使用并且可以和其它对象进行交互的对象。在.NET Framework中,组件是指实现System.ComponentModel.IComponent接口的类,或从实现IComponent的类中直接或间接派生的类。实际上,在VS.NET环境下开发的类(类库)如果生成为.dll后缀的文件,这些类就转变为组件了。
2.控件
控件是提供或实现用户界面功能的组件。.NET Framework为控件提供两个基类System.Windows.Forms.Control用于Windows窗体控件;System.Web.UI.Control用于web窗体控件。
3.属性
属性利用get和set方法提供私有成员与外界的交互。
4.特性
在.NET框架中,特性(Attribute)是一种标记,用于提供属性或方法的一些信息。特性声明在方括号中,在属性声明的上方,可以直接声明特性。
例如:
//默认值
[DefaultValue(null)]
//属性窗口底部的描述以及其所在分组的分类名称
[Description("校验错误时的提示文本信息。"), Category("验证")]
public string ErrorMessage
{
get{ ......
}
set{ ......
}
}
简单组件设计
1) 在VS2005中,选择【新建项目】,在【项目类型】中选择【Visual C#项目】,在【模板】中选择【类库】,输入文件名TestComponent,选择相应保存路径,完成上述操作后单击【确定】,进入代码编辑窗口。
2) 将源代码改为如下内容。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace TestComponent
{
public class MyDataBase
{ //返回表中记录数
public int GetRecordCount(string connString, string tableName)
{
int number = -1;
SqlConnection conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand("select count(*) from " + tableName, conn);
try {
conn.Open();
number = (int)command.ExecuteScalar();
conn.Close();
}
catch (Exception err)
{
throw new Exception(err.Message);
}
return number;
}
//根据Select语句自动生成其他SQL语句
public void BuildAdapter(ref SqlDataAdapter adapter)
{
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.DeleteCommand = builder.GetDeleteCommand();
adapter.InsertCommand = builder.GetInsertCommand();
adapter.UpdateCommand = builder.GetUpdateCommand();
}
}
}
Windows应用程序中组件调用
public Form1()
{
InitializeComponent();
string connString = Properties.Settings.Default.MyDatabaseConnectionString;
TestComponent.MyDataBase me = new TestComponent.MyDataBase();
label1.Text = "总记录数:" + me.GetRecordCount(connString, "MyTable2");
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter("select * from MyTable2", conn);
me.BuildAdapter(ref adapter);
DataSet ds = new DataSet();
adapter.Fill(ds, "MyTable2");
dataGridView1.DataSource = ds.Tables["MyTable2"];
}
用户控件
控件有用户控件、自定义控件和扩展控件三种形式。对于Windows窗体,用户控件默认继承自System.Windows.Forms.UserControl。用户控件适用于直接在Form窗口中添加系统自带的控件并为其编写相应的代码。
例:设计一个Windows用户控件,该用户控件在Windows应用程序中完成与Web服务器控件RegularExpressionVlidator相似的验证功能。其中有一个文本框验证控件和一个标签控件。该控件自定义三个供公开使用的属性:RegexText、ErrorMessage和ValidationExpression。用户可以验证该控件的输入值RegexText是否匹配正则表达式指定的模ValidationExpression
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace RegexValidator
{
public partial class RegexValidator : UserControl
{
private string validationExpression;
//属性RegexText的定义,归类到“验证”分组
[Description("与控件关联的文本信息。"),Category("验证")]
public string RegexText
{
get
{
return textBoxRegex.Text;
}
set
{
textBoxRegex.Text = value;
}
}
//属性ErrorMessage的定义,归类到“验证”分组
[Description("校验错误时的文本提示信息。"), Category("验证")]
public string ErrorMessage
{
get
{
return labelRegex.Text;
}
set
{
labelRegex.Text = value;
}
}
//属性ValidationExpression的定义,归类到“验证”分组
[DefaultValue(null)]
[Description("指定验证时的正则表达式。"), Category("验证")]
public string ValidationExpression
{
get
{
return validationExpression;
}
set
{
try
{
Regex r = new Regex(value);
validationExpression = value;
}
catch
{
MessageBox.Show(this,"正则表达式语法错误!","错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
validationExpression = null;
}
}
}
//当焦点从文本框离开时执行的验证操作
private void textBoxRegex_Leave(object sender, EventArgs e)
{
labelRegex.Visible = !Regex.IsMatch(textBoxRegex.Text, validationExpression);
if (labelRegex.Visible)
{
MessageBox.Show("请按要求输入", "", MessageBoxButtons.OK,
MessageBoxIcon.Hand);
textBoxRegex.Focus();
}
else
{
MessageBox.Show("注册验证成功", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
自定义控件
自定义控件相对用户控件复杂些;Windows窗体的自定义控件默认从system.Windows.Forms.Control类继承,即完全自定义控件。除了默认继承的情况外,也可以修改自定义控件,让其继承自工具箱中已有的控件,这样可以省略很多设计工作。比如创建自定义控件后,可以将System.Windows.Forms.Control改Syatem.Windows.Forms.Button,这样就可以在Button控件的基础上进一步制作,比如修改控件的外观等。
首先设计自定义控件(新建项目--Windows控件库--项目--添加新项--自定义控件--最后生成的是.dll文件);然后将自定义控件添加到工具箱中。
例:创建自定义控件CustomButton,使用DrawButton方法来绘制用户界面,单击此控件则在原始控件的边界内绘制一个较小的CustomButton按钮,以此来完成类似于Button按钮的视觉效果。
private Rectangle clickRectangleValue = new Rectangle();
//指定用视觉样式绘制的按钮的视觉状态
private PushButtonState state = PushButtonState.Normal;
//定义单击按钮后的小按钮矩形边界
public Rectangle ClickRectangle
{
get
{
clickRectangleValue.X = ClientRectangle.X +
(int)(.2 * ClientRectangle.Width);
clickRectangleValue.Y = ClientRectangle.Y +
(int)(.2 * ClientRectangle.Height);
clickRectangleValue.Width = ClientRectangle.Width -
(int)(.4 * ClientRectangle.Width);
clickRectangleValue.Height = ClientRectangle.Height -
(int)(.4 * ClientRectangle.Height);
return clickRectangleValue;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
// 如果按钮被按下去,则绘制小的按钮图形界面
if (state == PushButtonState.Pressed)
{
ButtonRenderer.DrawParentBackground(pe.Graphics, ClientRectangle, this);
ButtonRenderer.DrawButton(pe.Graphics, ClickRectangle, this.Text, this.Font, true, state);
}
//绘制大的未被按下去的按钮图形界面
else
{
ButtonRenderer.DrawButton(pe.Graphics, ClientRectangle, this.Text, this.Font, false, state);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
//设置按钮视觉状态为按下状态
state = PushButtonState.Pressed;
Invalidate();
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
//设置按钮状态为激活状态
state = PushButtonState.Hot;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
state = PushButtonState.Normal;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
OnMouseEnter(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// 鼠标左键按下并且光标从按下的按钮上离开时,
// 恢复按钮的正常外观。
if ((e.Button & MouseButtons.Left) == MouseButtons.Left &&
!ClientRectangle.Contains(e.Location) &&
state == PushButtonState.Pressed)
{
OnMouseLeave(e);
}
}
扩展控件(略)扩展控件是指从任何现有的Windows窗体控件或者自定义控件导出继承控件;实际上扩展控件是一种继承的自定义控件。总结:组件的文件格式是.dll,可以复用并可以和其它对象进行交互。详细内容参看《C#网络编程及应用》刘瑞新主编 机械工业出版社
1. 组件
组件是指可重复使用并且可以和其它对象进行交互的对象。在.NET Framework中,组件是指实现System.ComponentModel.IComponent接口的类,或从实现IComponent的类中直接或间接派生的类。实际上,在VS.NET环境下开发的类(类库)如果生成为.dll后缀的文件,这些类就转变为组件了。
2.控件
控件是提供或实现用户界面功能的组件。.NET Framework为控件提供两个基类System.Windows.Forms.Control用于Windows窗体控件;System.Web.UI.Control用于web窗体控件。
3.属性
属性利用get和set方法提供私有成员与外界的交互。
4.特性
在.NET框架中,特性(Attribute)是一种标记,用于提供属性或方法的一些信息。特性声明在方括号中,在属性声明的上方,可以直接声明特性。
例如:
//默认值
[DefaultValue(null)]
//属性窗口底部的描述以及其所在分组的分类名称
[Description("校验错误时的提示文本信息。"), Category("验证")]
public string ErrorMessage
{
get{ ......
}
set{ ......
}
}
简单组件设计
1) 在VS2005中,选择【新建项目】,在【项目类型】中选择【Visual C#项目】,在【模板】中选择【类库】,输入文件名TestComponent,选择相应保存路径,完成上述操作后单击【确定】,进入代码编辑窗口。
2) 将源代码改为如下内容。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace TestComponent
{
public class MyDataBase
{ //返回表中记录数
public int GetRecordCount(string connString, string tableName)
{
int number = -1;
SqlConnection conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand("select count(*) from " + tableName, conn);
try {
conn.Open();
number = (int)command.ExecuteScalar();
conn.Close();
}
catch (Exception err)
{
throw new Exception(err.Message);
}
return number;
}
//根据Select语句自动生成其他SQL语句
public void BuildAdapter(ref SqlDataAdapter adapter)
{
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.DeleteCommand = builder.GetDeleteCommand();
adapter.InsertCommand = builder.GetInsertCommand();
adapter.UpdateCommand = builder.GetUpdateCommand();
}
}
}
Windows应用程序中组件调用
public Form1()
{
InitializeComponent();
string connString = Properties.Settings.Default.MyDatabaseConnectionString;
TestComponent.MyDataBase me = new TestComponent.MyDataBase();
label1.Text = "总记录数:" + me.GetRecordCount(connString, "MyTable2");
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter("select * from MyTable2", conn);
me.BuildAdapter(ref adapter);
DataSet ds = new DataSet();
adapter.Fill(ds, "MyTable2");
dataGridView1.DataSource = ds.Tables["MyTable2"];
}
用户控件
控件有用户控件、自定义控件和扩展控件三种形式。对于Windows窗体,用户控件默认继承自System.Windows.Forms.UserControl。用户控件适用于直接在Form窗口中添加系统自带的控件并为其编写相应的代码。
例:设计一个Windows用户控件,该用户控件在Windows应用程序中完成与Web服务器控件RegularExpressionVlidator相似的验证功能。其中有一个文本框验证控件和一个标签控件。该控件自定义三个供公开使用的属性:RegexText、ErrorMessage和ValidationExpression。用户可以验证该控件的输入值RegexText是否匹配正则表达式指定的模ValidationExpression
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace RegexValidator
{
public partial class RegexValidator : UserControl
{
private string validationExpression;
//属性RegexText的定义,归类到“验证”分组
[Description("与控件关联的文本信息。"),Category("验证")]
public string RegexText
{
get
{
return textBoxRegex.Text;
}
set
{
textBoxRegex.Text = value;
}
}
//属性ErrorMessage的定义,归类到“验证”分组
[Description("校验错误时的文本提示信息。"), Category("验证")]
public string ErrorMessage
{
get
{
return labelRegex.Text;
}
set
{
labelRegex.Text = value;
}
}
//属性ValidationExpression的定义,归类到“验证”分组
[DefaultValue(null)]
[Description("指定验证时的正则表达式。"), Category("验证")]
public string ValidationExpression
{
get
{
return validationExpression;
}
set
{
try
{
Regex r = new Regex(value);
validationExpression = value;
}
catch
{
MessageBox.Show(this,"正则表达式语法错误!","错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
validationExpression = null;
}
}
}
//当焦点从文本框离开时执行的验证操作
private void textBoxRegex_Leave(object sender, EventArgs e)
{
labelRegex.Visible = !Regex.IsMatch(textBoxRegex.Text, validationExpression);
if (labelRegex.Visible)
{
MessageBox.Show("请按要求输入", "", MessageBoxButtons.OK,
MessageBoxIcon.Hand);
textBoxRegex.Focus();
}
else
{
MessageBox.Show("注册验证成功", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
自定义控件
自定义控件相对用户控件复杂些;Windows窗体的自定义控件默认从system.Windows.Forms.Control类继承,即完全自定义控件。除了默认继承的情况外,也可以修改自定义控件,让其继承自工具箱中已有的控件,这样可以省略很多设计工作。比如创建自定义控件后,可以将System.Windows.Forms.Control改Syatem.Windows.Forms.Button,这样就可以在Button控件的基础上进一步制作,比如修改控件的外观等。
首先设计自定义控件(新建项目--Windows控件库--项目--添加新项--自定义控件--最后生成的是.dll文件);然后将自定义控件添加到工具箱中。
例:创建自定义控件CustomButton,使用DrawButton方法来绘制用户界面,单击此控件则在原始控件的边界内绘制一个较小的CustomButton按钮,以此来完成类似于Button按钮的视觉效果。
private Rectangle clickRectangleValue = new Rectangle();
//指定用视觉样式绘制的按钮的视觉状态
private PushButtonState state = PushButtonState.Normal;
//定义单击按钮后的小按钮矩形边界
public Rectangle ClickRectangle
{
get
{
clickRectangleValue.X = ClientRectangle.X +
(int)(.2 * ClientRectangle.Width);
clickRectangleValue.Y = ClientRectangle.Y +
(int)(.2 * ClientRectangle.Height);
clickRectangleValue.Width = ClientRectangle.Width -
(int)(.4 * ClientRectangle.Width);
clickRectangleValue.Height = ClientRectangle.Height -
(int)(.4 * ClientRectangle.Height);
return clickRectangleValue;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
// 如果按钮被按下去,则绘制小的按钮图形界面
if (state == PushButtonState.Pressed)
{
ButtonRenderer.DrawParentBackground(pe.Graphics, ClientRectangle, this);
ButtonRenderer.DrawButton(pe.Graphics, ClickRectangle, this.Text, this.Font, true, state);
}
//绘制大的未被按下去的按钮图形界面
else
{
ButtonRenderer.DrawButton(pe.Graphics, ClientRectangle, this.Text, this.Font, false, state);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
//设置按钮视觉状态为按下状态
state = PushButtonState.Pressed;
Invalidate();
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
//设置按钮状态为激活状态
state = PushButtonState.Hot;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
state = PushButtonState.Normal;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
OnMouseEnter(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// 鼠标左键按下并且光标从按下的按钮上离开时,
// 恢复按钮的正常外观。
if ((e.Button & MouseButtons.Left) == MouseButtons.Left &&
!ClientRectangle.Contains(e.Location) &&
state == PushButtonState.Pressed)
{
OnMouseLeave(e);
}
}
扩展控件(略)扩展控件是指从任何现有的Windows窗体控件或者自定义控件导出继承控件;实际上扩展控件是一种继承的自定义控件。总结:组件的文件格式是.dll,可以复用并可以和其它对象进行交互。详细内容参看《C#网络编程及应用》刘瑞新主编 机械工业出版社