最近同事让我帮着做一个生日提醒的小软件,我上网找了半天也没有找到一个有源码的程序。于是就想着自己做一个吧,功能反正也不复杂。开发这种带界面的小程序,用C#开发速度应该比较快,我正好也要学习一下C#编程技术,所以按照所需的功能自己用C#做了一个生日提醒的软件。虽然不是很美观,但基本功能都实现了,这里总结一下其中所用到的编程技巧。
(1)建立Access数据库连接
Step1:声明数据库操作的命名空间
using System.Data.OleDb;
Step 2:查询数据库中的字段内容,代码如下:
//数据库连接字符串
string strDBCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data source = '生日.mdb'";
//创建数据库连接
OleDbConnection DBCon = new OleDbConnection();
DBCon.ConnectionString = strDBCon;
//打开数据库
try
{
//打开数据库
DBCon.Open();
m_ListPerson = new List<PersonInfo>();
//查询目标数据
string strSQL = "select * from 个人信息";
OleDbCommand cmd = new OleDbCommand(strSQL, DBCon);
OleDbDataReader dbReader = cmd.ExecuteReader();
//读取所有的信息到链表中
while(dbReader.Read())
{
string strName;
int nYear;
int nMonth;
int nDay;
string strContent;
string strTel;
string strPlace;
//按照数据库中的字段顺序,读取对应的信息
//姓名
strName = dbReader.GetString(1);//参数代表该字段在表中的位置
//日期
nYear = dbReader.GetInt32(2);//转换为Int型数据
nMonth = dbReader.GetInt32(3);
nDay = dbReader.GetInt32(4);
//备注
strTel = dbReader.GetString(5);//转换为字符串型
//联系方式
strContent = dbReader.GetString(6);
//地区
strPlace = dbReader.GetString(7);
}
}
catch (System.Exception ex)
{
ex.GetBaseException();
}
finally
{
DBCon.Close();
}
(2)使用<List>链表
Step 1: 声明命名空间
using System.Collections.Generic;
Step 2:定义链表变量
private List<PersonInfo> m_ListPerson = null;
Step 3:向链表中添加元素
m_ListPerson.Add(newInfo);
(3)ListControl列表控件的使用
Step 1:在设计界面下,将列表控件拖到Form上适当的位置。在控件上右键单击,选择“属性”。在属性列表中可以更改控件变量的名称,比如BirthdayList。
Step 2:在向列表控件添加列表项之前,必须为控件添加列。
this. BirthdayList.Clear();//情况所有列表项
this. BirthdayList.Columns.Add("姓名", 50, HorizontalAlignment.Center);//第一个参数是列的名称,第二个参数是列的宽度,第三个参数是对齐方式。
Step 3:添加列表项内容
//定义列表项
ListViewItem tItem = new ListViewItem();
//按顺序添加每一列的内容
tItem.SubItems.Add( strDate);
tItem.SubItems.Add(m_ListPerson[i].m_strPlace);
tItem.SubItems.Add(m_ListPerson[i].m_strContent);
tItem.SubItems.Add(m_ListPerson[i].m_strTel);
//将列表项内容添加到列表控件中
this.BirthdayList.Items.Add(tItem);
(4)最小化程序图标到系统托盘,并显示气泡提示
Step 1:定义系统托盘图标控件
private System.Windows.Forms.NotifyIcon notifyIcon;//系统托盘控件
Step 2:初始化图标控件
notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);//创建资源
notifyIcon.Visible = false;//初始化为不可见
notifyIcon.Icon = new Icon("Game.ico");//指定图标资源
Step 3:添加点击响应函数和鼠标移动响应函数
notifyIcon.Click += new System.EventHandler(this.notifyIcon_Click);
notifyIcon.MouseMove += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseMove);
Step 4:定义响应函数
//系统托盘图标控件的点击事件响应函数
private void notifyIcon_Click(object Sender, EventArgs e)
//单击系统栏图标激活窗体
{
if (this.WindowState == FormWindowState.Minimized)//如果窗体的状态为最小化
this.WindowState = FormWindowState.Normal;//将窗体恢复原状
this.Activate();//激活当前窗口
this.notifyIcon.Visible = false;//隐藏图标
this.ShowInTaskbar = true;//将窗体在系统栏上显示
}
//当鼠标放在图标上的响应函数
private void notifyIcon_MouseMove(object sender,MouseEventArgs e)
{
string strName = "";
this.notifyIcon.ShowBalloonTip(1000,”提示内容标题”, “提示内容”, ToolTipIcon.Info);//显示气泡提示,第一个参数为气泡提示显示的时间长度,后面依次是标题、内容和气泡图标类型
}
Step 5:在窗体最小化时显示托盘图标
首先在设计视图中,为Form添加SizeChanged响应函数MainForm_SizeChanged
然后在该函数体中,将托盘图标显示出来
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)//最小化
{
this.ShowInTaskbar = false;//在系统栏上隐藏窗体
this.notifyIcon.Visible = true;//显示托盘图标
}
}
(5)定时器的使用
Step 1:在Form类中添加成员
private System.Windows.Forms.Timer MyTimer = null;
Step 2:在Form类的构造函数中添加
MyTimer = new System.Windows.Forms.Timer();/.创建资源
MyTimer.Start();//开始计时器
MyTimer.Enabled = true;//启动计时器
MyTimer.Tick += new System.EventHandler(MyTimer_Tick);//为计时器添加触发响应函数
Step 3:添加定时器触发函数MyTimer_Tick
(6)文字动画
Step 1: 在设计视图下,为Form添加一个Label控件,将该控件的名称改为MovingTxt
Step 2:为控件指定文字信息
this.MovingTxt.Text = "最近没有人过生日!";
Step 3:在程序初始化的时候初始化控件的位置
this.MovingTxt.Left = 0;//Left表示控件离父窗口左边缘的位置
Step 4:在计时器触发函数中改变this.MovingTxt.Left的值就可以实现动画效果。通过改变计时器的触发频率和控件移动的步长来控制控件移动速度。
(7)将列表中的内容按列排序
Step 1:添加进行列表控件内容比较的类
class ListViewItemComparer : IComparer
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
}
Step 2:在设计视图中,为列表控件添加列点击消息响应函数BirthdayList_ColumnClick
private void BirthdayList_ColumnClick(object sender, ColumnClickEventArgs e)
{
this.BirthdayList.ListViewItemSorter = new ListViewItemComparer(e.Column);
}
(8)更改程序的图标
在工程上点击右键,打开属性,就可以看到下面界面,可以指定应用程序的图标。