本程序的基本框架代码或者说大部分代码是来自北风网老师的C#家庭理财项目的那个视频。
自己在这实现了一遍,其中做了一点优化,其中很多地方存在问题,以及没有进行所有功能的实现。
所有代码(带详细注释)如下:
下面是登陆框界面的代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Finace { public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); } private void btnRegister_Click(object sender, EventArgs e) { RegisterForm rForm = new RegisterForm(); rForm.lg = this; //将当前窗口传递给注册窗口 rForm.Show(); } /// <summary> /// 功能:用来接收注册页面传递来的用户,并显示在文本框上 /// 时间:2013/9/15 /// 作者:余颜凯 /// </summary> /// <param name="name"></param> public void getUserName(string name) { textName.Text = name; } //获得保存的密码 public void getPassword() { string sql = string.Format("select * from SavePasswords where Id=1"); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DBOperate.connection.Open(); DataSet ds = new DataSet(); //全部查询 SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); textName.Text = sdr["UserName"].ToString(); textPassword.Text = sdr["UserPassword"].ToString(); sdr.Close(); DBOperate.connection.Close(); } //保存用户名和密码到数据库的SavePasswords表 public void savePassword() { //保存的永远只有一条,所以用update string sql = string.Format("update SavePasswords set UserName='{0}',UserPassword='{1}' where Id=1", textName.Text.Trim(), textPassword.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DBOperate.connection.Open(); cmd.ExecuteNonQuery(); DBOperate.connection.Close(); } /// <summary> /// 不足:没用重用登陆按钮的功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, EventArgs e) { bool isNotEmpty = CheckEmpty(); //如果能通过检查, if (isNotEmpty == true) { //查询数量即可 string sql = string.Format("select count(*) from Users where UserName='{0}' and UserPassword = '{1}'", textName.Text.Trim(), textPassword.Text.Trim()); DBOperate.connection.Open(); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); //关键字User 附近有语法错误------表的名字不要起系统默认的名字(系统关键字) //int count = (int)cmd.ExecuteScalar(); //首行查询 int count = Convert.ToInt32(cmd.ExecuteScalar()); DBOperate.connection.Close(); if (count > 0) { if (ckbRemember.Checked == true) { this.savePassword(); } //登陆成功就打开主窗口 MainForm mForm = new MainForm(); mForm.Show(); //只能因此父窗口(LoginForm) this.Hide(); } else//数据库中无数据 { labelAllError.Visible = true; } } } private bool CheckEmpty() { //默认为不为空 bool result = true; if (textName.Text.Trim() == String.Empty) { labelNameError.Visible = true; result = false; } else { labelNameError.Visible = false; } if (textPassword.Text.Trim() == String.Empty) { labelPasswordError.Visible = true; result = false; } else { labelPasswordError.Visible = false; } return result; } /// <summary> /// 功能:在密码的对话框中就能进行回车登陆 /// 时间:2013/9/21 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textPassword_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { bool isNotEmpty = CheckEmpty(); //如果能通过检查, if (isNotEmpty == true) { //查询数量即可 string sql = string.Format("select count(*) from Users where UserName='{0}' and UserPassword = '{1}'", textName.Text.Trim(), textPassword.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DBOperate.connection.Open(); //关键字User 附近有语法错误------表的名字不要起系统默认的名字(系统关键字) int count = (int)cmd.ExecuteScalar(); DBOperate.connection.Close(); if (count > 0) { if (ckbRemember.Checked == true) { this.savePassword(); } //登陆成功就打开主窗口 MainForm mForm = new MainForm(); mForm.Show(); //只能因此父窗口(LoginForm) this.Hide(); } else//数据库中无数据 { labelAllError.Visible = true; } } } } private void LoginForm_Load(object sender, EventArgs e) { getPassword(); } } }
注册窗口的代码入下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Finace { public partial class RegisterForm : Form { //用来保存登陆窗口,便于跨窗口传送数据 public LoginForm lg; public RegisterForm() { InitializeComponent(); } private void btnRegister_Click(object sender, EventArgs e) { bool isNotEmpty = CheckEmpty(); //如果能通过检查, if (isNotEmpty == true) { if (getResist() == true) { MessageBox.Show("用户已经注册,请重新输入用户名!"); textName.SelectAll(); textName.Focus(); } else { //User表插入数据 string sql = string.Format("insert into Users Values('{0}','{1}')", textName.Text.Trim(), textPassword.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DBOperate.connection.Open(); //关键字User 附近有语法错误------表的名字不要起系统默认的名字(系统关键字) int count = cmd.ExecuteNonQuery(); DBOperate.connection.Close(); if (count == 1) { MessageBox.Show("成功注册了一个用户"); lg.getUserName(textName.Text.Trim()); //lg.savePassword(); this.Close(); } } } } /// <summary> /// 功能:检验是否已经注册 /// 时间:2013/9/21 /// 作者:余颜凯 /// </summary> /// <returns></returns> public bool getResist() { bool isRegist = false; string sql = string.Format("select count(*) from Users where UserName='{0}'", textName.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DBOperate.connection.Open(); int result = Convert.ToInt32(cmd.ExecuteScalar()); DBOperate.connection.Close(); if (result == 1) { isRegist = true; } return isRegist; } /// <summary> /// 功能:判断注册页面文本框是否为空 /// 时间:2013/9/15 /// 作者:余颜凯 /// </summary> /// <returns></returns> private bool CheckEmpty() { //默认为不为空 bool result = true; if (textName.Text.Trim() == String.Empty) { labelNameError.Visible = true; result= false; } else { labelNameError.Visible = false; } if (textPassword.Text.Trim() == String.Empty) { labelPasswordError.Visible = true; result= false; } else { labelPasswordError.Visible = false; } if (textConfirmPassword.Text.Trim() == String.Empty) { labelConfirmError.Visible = true; result= false; } else { //验证密码不为空,但是又不相同 if (textPassword.Text.Trim() != textConfirmPassword.Text.Trim()) { //MessageBox.Show("两次输入的密码不一致"); labelConfirmError.Text = " 两次密码不同"; labelConfirmError.Visible = true; result = false; } else { //labelConfirmError.Text = " 密码不能为空"; labelConfirmError.Visible = false; } } return result; } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } } }
主窗口的代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Collections; namespace Finace { public partial class MainForm : Form { //用于保存当前窗体大小 // private int FormHeight = 0; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“financeProjectDataSet.Consume”中,可以根据需要移动或删除它。 this.consumeTableAdapter.Fill(this.financeProjectDataSet.Consume); comboBoxSort.SelectedIndex = 0; comboBoxDir.SelectedIndex = 0; //取消默认选中第一行 dataViewControl.ClearSelection(); //隐藏面板的 //开始的时候让底部面板隐藏 panelEdit.Visible = false; this.btnHide.Location = new Point(this.btnHide.Location.X, this.btnHide.Location.Y-134); this.Size = new Size(this.Width, 533); comboBoxLei.SelectedIndex = 0; comboBoxItem.SelectedIndex = 0; //加载消费条目时间 for (int i = 1992; i < 2092; i++) { comboBoxDateYear.Items.Add(i.ToString()); } for (int i = 1; i <= 12; i++) { comboBoxDateMonth.Items.Add(i.ToString()); } for (int i = 1; i <= 31; i++) { comboBoxDate.Items.Add(i.ToString()); } //加载总收入和支出---------后面证明,它得重用,所以写成函数 //double income=0, consume=0; //consume = getTotalMoney("支出"); //income = getTotalMoney("收入"); //textBoxIn.Text = income.ToString(); //textBoxOut.Text = consume.ToString(); //textBoxAll.Text = (income - consume).ToString(); showTotalMoney(); //改变textBox里字体颜色时,前景和背景色全部要改变! //if ((income - consume) >= 0) //{ // textBoxAll.ForeColor = System.Drawing.Color.Lime; //} //else // textBoxAll.ForeColor = System.Drawing.Color.Red; btnDelete.Enabled = false; } /// <summary> /// 功能:显示当前的时间 /// 时间:2013/9/16 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timeDate_Tick(object sender, EventArgs e) { DateTime dt = DateTime.Now; String date = dt.ToLongDateString(); String time = dt.ToLongTimeString(); labelTime.Text = date + time; } /// <summary> /// 功能:隐藏或者显示底部编辑框架 /// 时间:2013/9/20 /// 作者:余颜凯 /// 补充:后来又将此函数的功能单独写成一个函数了 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnHide_Click(object sender, EventArgs e) { //667 //134 //if (this.Size.Height == 667)//之前是展开的窗口,点该按钮后变成收起状态 //{ // this.Size = new Size(this.Width, 533); // panelEdit.Visible = false; // this.btnHide.Location = new Point(this.btnHide.Location.X, this.btnHide.Location.Y - 134); // this.btnHide.Text = "详细信息"; //} //else //{ // this.Size = new Size(this.Width, 667); // panelEdit.Visible = true; // this.btnHide.Text = "收起信息"; // this.btnHide.Location = new Point(this.btnHide.Location.X, this.btnHide.Location.Y + 134); //} //上面代码已经被浓缩成函数(方便重用) //默认的时候是隐藏的,即该按钮的文字是"详细信息" if (btnHide.Text == "详细信息") { hidePanel(false); } else hidePanel(true); } /// <summary> /// 功能:隐藏窗口 /// 时间:2013/9/21 /// 作者:余颜凯 /// </summary> private bool hidePanel(bool hide) { //传入的hide如果为true表明是要隐藏现在这个窗口 if (hide == true) { this.Size = new Size(this.Width, 533); panelEdit.Visible = false; this.btnHide.Location = new Point(this.btnHide.Location.X, this.btnHide.Location.Y - 134); this.btnHide.Text = "详细信息"; panelEdit.Enabled = false; } else { this.Size = new Size(this.Width, 667); panelEdit.Visible = true; this.btnHide.Text = "收起信息"; this.btnHide.Location = new Point(this.btnHide.Location.X, this.btnHide.Location.Y + 134); return false; } return true; } /// <summary> /// 功能:低端下拉窗口的修改 /// 时间:2013/9/20 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnEdit_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=FinanceProject;User Id =sa; Password=merlin"); string date =comboBoxDateYear.Text.Trim() + "/" + comboBoxDateMonth.Text.Trim() + "/" + comboBoxDate.Text.Trim(); //Money和id不需要加上引号(update语句) string sql = string.Format("update consume set ConsumeDate='{0}',Type='{1}',Catagory='{2}',Money={3},Description='{4}'where id={5}", date, comboBoxLei.Text, comboBoxItem.Text, textBoxJin.Text.Trim(), textBoxDescription.Text.Trim(),textBoxLiu.Text.Trim()); //insert语句 //string sql = string.Format("insert into consume values('{0}','{1}','{2}','{3}','{4}','{5}')", textBoxLiu.Text.Trim(), // date,comboBoxLei,comboBoxItem,textBoxJin.Text.Trim(),textBoxDescription.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, conn); //打开数据库 conn.Open(); int rowsNums = cmd.ExecuteNonQuery(); if (rowsNums > 0) { MessageBox.Show("更新成功"); //但是dataGridView并没有刷新显示 GetGridviewAll(); showTotalMoney(); } else { MessageBox.Show("更新失败"); } //关闭数据库 conn.Close(); } /// <summary> /// 功能:在下拉框中显示选中的详细信息 /// 时间:2013/9/20 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataViewControl_CellClick(object sender, DataGridViewCellEventArgs e) { //this.panelEdit.Enabled = true; btnDelete.Enabled = true; toolStripButtonDelete.Enabled = true; //将隐藏框里的文字和图片换掉 labelItemSelected.Text = "已选中一条记录"; //显示图像 //1.尝试失败 //pictureBoxItemSelected.BackgroundImage.Dispose(); pictureBoxItemSelected.SizeMode = PictureBoxSizeMode.StretchImage; //pictureBoxItemSelected.BackgroundImage = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory+"..\\..\\images\\ItemSelected.png"); pictureBoxItemSelected.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\images\\ItemSelected.png"); //显示选中行的信息 //流水号 textBoxLiu.Text = dataViewControl.SelectedRows[0].Cells[0].Value.ToString(); //日期 string actDate = dataViewControl.SelectedRows[0].Cells[1].Value.ToString(); //在dataView中显示的是1999/9/9 (获取的格式也是这样) //分离出 年、月、日 DateTime dt = Convert.ToDateTime(actDate); //对于combox的显示项目,可以用Item. comboBoxDateYear.Text = dt.Year.ToString().Trim(); comboBoxDateMonth.Text = dt.Month.ToString().Trim(); comboBoxDate.Text = dt.Day.ToString().Trim(); //选择收入还是支出 if ("收入" == dataViewControl.SelectedRows[0].Cells[2].Value.ToString().Trim()) { comboBoxLei.SelectedIndex = 0; } else comboBoxLei.SelectedIndex = 1; //收支项目,多次测验发现,必须写已有的固定项目,才能正确显现 comboBoxItem.SelectedItem = dataViewControl.SelectedRows[0].Cells[3].Value.ToString().Trim(); //金额 textBoxJin.Text = dataViewControl.SelectedRows[0].Cells[4].Value.ToString().Trim(); //作为收支项目的补充 textBoxDescription.Text = dataViewControl.SelectedRows[0].Cells[5].Value.ToString().Trim(); } /// <summary> /// 功能:选中收入还是支出的时候给出相应的项目变化 /// 时间:2012/9/20 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBoxLei_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxLei.SelectedIndex == 0)//选中的为收入 { comboBoxItem.Items.Clear();//清空收入 comboBoxItem.Items.Add("兼职"); comboBoxItem.Items.Add("银行利息"); comboBoxItem.Items.Add("基金分红"); comboBoxItem.Items.Add("奖学金"); comboBoxItem.Items.Add("其他收入"); } else //如果选中为支出 { comboBoxItem.Items.Clear(); comboBoxItem.Items.Add("娱乐"); comboBoxItem.Items.Add("吃饭"); comboBoxItem.Items.Add("学习"); comboBoxItem.Items.Add("购物"); } comboBoxItem.SelectedIndex = 0; } /// <summary> /// 功能:获得总的收入和支出 /// 时间:2013/9/20 /// 作者:余颜凯 /// </summary> /// <param name="type"></param> /// <returns></returns> private double getTotalMoney(string type) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=FinanceProject;User Id =sa; Password=merlin"); string sql = string.Format("select sum(Money)from Consume where[type]='{0}'",type); SqlCommand cmd = new SqlCommand(sql,conn); //打开数据库 conn.Open(); //执行数据库命令cmd,返回首行首列,只返回一行 //double total = (double)cmd.ExecuteScalar(); double total ; //从数据库里面拿到的值是空值但是转换成另一种类型的时候要求不能是空值---baidu.com //try //{ // total = Convert.ToDouble(cmd.ExecuteScalar()); //} //catch (Exception e) //{ // MessageBox.Show(e.ToString()); //} if (cmd.ExecuteScalar() != System.DBNull.Value) { total = Convert.ToDouble(cmd.ExecuteScalar()); } else total = 0; //关闭数据库 conn.Close(); return total; } /// <summary> /// 功能:主界面上总金额显示功能打包,方便其他类重用 /// 时间:2013/9/21 /// 作者:余颜凯 /// </summary> public void showTotalMoney() { double income = 0, consume = 0; consume = getTotalMoney("支出"); income = getTotalMoney("收入"); textBoxIn.Text = income.ToString(); textBoxOut.Text = consume.ToString(); textBoxAll.Text = (income - consume).ToString(); if ((income - consume) >= 0) { textBoxAll.ForeColor = System.Drawing.Color.Lime; } else textBoxAll.ForeColor = System.Drawing.Color.Red; } /// <summary> /// 功能:将刷新dataView的功能封装 /// 时间:2013/9/21 /// 作者:余颜凯 /// </summary> public void GetGridviewAll() { string sql = "select * from Consume "; SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DataSet ds = new DataSet(); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; sda.Fill(ds, "Consume"); dataViewControl.DataSource = ds.Tables[0]; //取消默认选中第一行 dataViewControl.ClearSelection(); //把删除键设为不可用 btnDelete.Enabled = false; toolStripButtonDelete.Enabled = false; } /// <summary> /// 跟上面一个规则只是选择语句不同 /// </summary> /// <param name="search"></param> public void SearchData(string search) { //查询数据结果 string sql = string.Format("select * from Consume where Type='{0}' ",search); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DataSet ds = new DataSet(); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; sda.Fill(ds, "search"); dataViewControl.DataSource = ds.Tables[0]; dataViewControl.ClearSelection(); } /// <summary> /// 功能:关闭该主界面,则隐藏的登陆主界面也同时关闭,并释放资源 /// 时间:2013/9/21 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //private void MainForm_FormClosing(object sender, FormClosingEventArgs e) //{ // if (MessageBox.Show(this, "确认关闭?", // "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) // { // ArrayList alist = new ArrayList(); // alist.Add("newCODDemo.exe"); //一旦有文件读写,关闭主程序,也要关闭文本文件该代码很有用 // alist.Add("newCODDemo.vshost.exe"); // for (int i = 0; i < alist.Count; i++) // { // if (System.Diagnostics.Process.GetProcessesByName(alist[i].ToString()).Length <= 0) // { // try // { // //System.Diagnostics.Process.Start(processName); // System.Diagnostics.Process.GetCurrentProcess().Kill(); // } // catch // { // MessageBox.Show(this, "关闭向导出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // } // } // } // } // else // { // e.Cancel = true; // } //} private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { //在关闭子窗口的同时还要关闭父窗口 //并且还要给出关闭子窗口的确认确认 DialogResult result = MessageBox.Show("请是否确认是否要退出系统!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); while (result == DialogResult.OK) { this.Dispose(); Application.Exit(); } { e.Cancel = true; } } //已修复---bug1:每一次增删查找后,就要对总收入,总支出,净收入进行重新计算 /// <summary> /// 不选中条目,按钮被提醒 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpdate_Click(object sender, EventArgs e) { //先选中条目记录,然后才谈修改不修改的问题 if (dataViewControl.SelectedRows.Count != 0) { //并且面板先前得是关闭的 if (this.Size.Height == 533) { //要修改必须先打开下面的操作条目面板 hidePanel(false); //每点一次这个修改按钮,那个"详细信息"按钮下移一次,功能不对 } this.panelEdit.Enabled = true; } else { MessageBox.Show("请您先选中一条记录,再来修改"); } } private void btnInsert_Click(object sender, EventArgs e) { AddForm addForm = new AddForm(); addForm.mf = this; addForm.ShowDialog(); } /// <summary> /// 换一钟写法:不选中项目,按钮不可用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDelete_Click(object sender, EventArgs e) { if (dataViewControl.SelectedRows.Count != 0) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=FinanceProject;User Id =sa; Password=merlin"); string sql = string.Format("delete from Consume where id={0}", textBoxLiu.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, conn); //打开数据库 conn.Open(); int rowsNums = cmd.ExecuteNonQuery(); if (rowsNums > 0) { MessageBox.Show("删除成功"); //但是dataGridView并没有刷新显示 showTotalMoney(); GetGridviewAll(); } else { MessageBox.Show("删除失败"); } //关闭数据库 conn.Close(); } //else //{ // MessageBox.Show("请您先选中一条记录,再来删除"); //} } private void btnFind_Click(object sender, EventArgs e) { SearchForm searchForm = new SearchForm(); searchForm.mf = this; searchForm.ShowDialog(); } /// <summary> /// 功能:菜单栏上的关于作者 /// 时间:2013/9/23 /// 作者: 余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToolStripMenuItem1_Click(object sender, EventArgs e) { AboutAuthor ab = new AboutAuthor(); ab.ShowDialog(); } /// <summary> /// 功能:菜单栏上的帮助里面的关于程序 /// 时间:2013/9/23 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToolStripMenuItem2_Click(object sender, EventArgs e) { MessageBox.Show(" 本程序是建立在北风网一个老师的C#家庭理财系统的基础上,仿制而成。" + "那个程序那老师并没有写完,并且也存在很多不合理和有问题的地方,按照他所讲的,进行了完善,补充,写出了本程序。" + "\n\n 虽然没有太多的原创新,不过在本次独立完成程序的过程中收获也是颇丰." + "\n\n 我并不在乎老师给我多少分儿, 我比较在意,老师拿什么眼光看我这样的行为," + "或者说看修改老师代码或者照着参考书敲程序的孩子。\n\n 最后,感谢很多人,很多事儿,我讨厌��嗦,不说了。", "程序说明"); } private void ToolStripMenuItemImport_Click(object sender, EventArgs e) { } private void ToolStripMenuItemExport_Click(object sender, EventArgs e) { } /// <summary> /// 功能:菜单栏上的退出 /// 时间:2013/9/23 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToolStripMenuItemQuit_Click(object sender, EventArgs e) { this.Dispose(); Application.Exit(); } /// <summary> /// 功能:工具栏上的关于作者 /// 时间:2013/9/23 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButton2_Click(object sender, EventArgs e) { AboutAuthor ab = new AboutAuthor(); ab.ShowDialog(); } /// <summary> /// 功能:工具栏上的增加按钮 /// 时间:2013/9/23 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButtonAdd_Click(object sender, EventArgs e) { AddForm addForm = new AddForm(); addForm.mf = this; addForm.ShowDialog(); } /// <summary> /// 工具栏上的查找 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButtonsearch_Click(object sender, EventArgs e) { SearchForm searchForm = new SearchForm(); searchForm.mf = this; searchForm.ShowDialog(); } /// <summary> /// 功能:工具栏上的删除键 /// 时间:2013/9/23 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButtonDelete_Click(object sender, EventArgs e) { if (dataViewControl.SelectedRows.Count != 0) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=FinanceProject;User Id =sa; Password=merlin"); string sql = string.Format("delete from Consume where id={0}", textBoxLiu.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, conn); //打开数据库 conn.Open(); int rowsNums = cmd.ExecuteNonQuery(); if (rowsNums > 0) { MessageBox.Show("删除成功"); //但是dataGridView并没有刷新显示 showTotalMoney(); GetGridviewAll(); } else { MessageBox.Show("删除失败"); } //关闭数据库 conn.Close(); } } /// <summary> /// 功能:工具栏上的修改按钮 /// 时间:2013/9/23 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButtonUpdate_Click(object sender, EventArgs e) { //先选中条目记录,然后才谈修改不修改的问题 if (dataViewControl.SelectedRows.Count != 0) { //并且面板先前得是关闭的 if (this.Size.Height == 533) { //要修改必须先打开下面的操作条目面板 hidePanel(false); //每点一次这个修改按钮,那个"详细信息"按钮下移一次,功能不对 } this.panelEdit.Enabled = true; } else { MessageBox.Show("请您先选中一条记录,再来修改"); } } } }
添加界面的代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Finace { public partial class AddForm : Form { //保留主菜单 public MainForm mf; public AddForm() { InitializeComponent(); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void AddForm_Load(object sender, EventArgs e) { comboBoxLei.SelectedIndex = 0; comboBoxItem.SelectedIndex = 0; //加载消费条目时间 for (int i = 1992; i < 2092; i++) { comboBoxDateYear.Items.Add(i.ToString()); } for (int i = 1; i <= 12; i++) { comboBoxDateMonth.Items.Add(i.ToString()); } for (int i = 1; i <= 31; i++) { comboBoxDate.Items.Add(i.ToString()); } } private void comboBoxLei_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxLei.SelectedIndex == 0)//选中的为收入 { comboBoxItem.Items.Clear();//清空收入 comboBoxItem.Items.Add("兼职"); comboBoxItem.Items.Add("银行利息"); comboBoxItem.Items.Add("基金分红"); comboBoxItem.Items.Add("奖学金"); comboBoxItem.Items.Add("其他收入"); } else //如果选中为支出 { comboBoxItem.Items.Clear(); comboBoxItem.Items.Add("娱乐"); comboBoxItem.Items.Add("吃饭"); comboBoxItem.Items.Add("学习"); comboBoxItem.Items.Add("购物"); } comboBoxItem.SelectedIndex = 0; } /// <summary> /// 功能:添加新收支记录 /// 时间:2013/9/21 /// 作者:余颜凯 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAdd_Click(object sender, EventArgs e) { //解决3件事儿:1.数据库,2.dataGridView 3.totalmoney //SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=FinanceProject;User Id =sa; Password=merlin"); // //insert语句 // //打开数据库 // conn.Open(); // int rowsNums = cmd.ExecuteNonQuery(); // if (rowsNums > 0) // { // MessageBox.Show("删除成功"); // //但是dataGridView并没有刷新显示 // showTotalMoney(); // } // else // { // MessageBox.Show("删除失败"); // } // //关闭数据库 // conn.Close(); bool isNull = IsNull();//判断是否为空 if (isNull) { string date = comboBoxDateYear.Text.Trim() + "/" + comboBoxDateMonth.Text.Trim() + "/" + comboBoxDate.Text.Trim(); //Money和id不需要加上引号(update语句) string sql = string.Format("insert into Consume values('{0}','{1}','{2}','{3}','{4}')", date,comboBoxLei.Text.Trim(),comboBoxItem.Text.Trim(),textBoxJin.Text.Trim(),textBoxDescription.Text.Trim()); SqlCommand cmd = new SqlCommand(sql, DBOperate.connection); DBOperate.connection.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("成功插入一条信息"); // TODO: 这行代码将数据加载到表“financeProjectDataSet.Consume”中,可以根据需要移动或删除它。 // this.consumeTableAdapter.Fill(this.financeProjectDataSet.Consume); mf.GetGridviewAll();//功能类似 mf.showTotalMoney(); DBOperate.connection.Close(); this.Close(); } else { MessageBox.Show("请按要求输入"); } } //判断是否为空 public bool IsNull() { bool isnull = true; if (comboBoxDateYear.Text.Trim() == string.Empty) { isnull = false; } if (comboBoxDateMonth.Text.Trim() == string.Empty) { isnull = false; } if (comboBoxDate.Text.Trim() == string.Empty) { isnull = false; } if (comboBoxLei.Text.Trim() == string.Empty) { isnull = false; } if (comboBoxItem.Text.Trim() == string.Empty) { isnull = false; } if (textBoxJin.Text.Trim() == string.Empty) { isnull = false; } return isnull; } } }
查找界面:
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Finace { public partial class SearchForm : Form { public MainForm mf; string ss = string.Empty; public SearchForm() { InitializeComponent(); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } /// <summary> /// 功能:单选框的文字判断查询方法,之后调用主界面的dataView显示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSearch_Click(object sender, EventArgs e) { if (ss == "全部") { mf.GetGridviewAll(); } else { mf.SearchData(ss); } this.Close(); } private void SearchForm_Load(object sender, EventArgs e) { ss = radioButtonIn.Text.Trim(); } private void radioButtonIn_CheckedChanged(object sender, EventArgs e) { ss = radioButtonIn.Text.Trim(); } private void radioButtonOut_CheckedChanged(object sender, EventArgs e) { ss = radioButtonOut.Text.Trim(); } private void radioButtonAll_CheckedChanged(object sender, EventArgs e) { ss = radioButtonAll.Text.Trim(); } } }
关于作者界面:
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Finace { public partial class AboutAuthor : Form { public AboutAuthor() { InitializeComponent(); } private void AboutAuthor_Load(object sender, EventArgs e) { //读取文件到控件textBoxIn StreamReader sr = new StreamReader("..\\..\\Properties\\SelfDescription.txt", Encoding.GetEncoding("GB2312")); //MessageBox.Show( "..\\"+ AppDomain.CurrentDomain.BaseDirectory ); //int n = 1; string temp = null; //获取一行信息 string totalText = null;//用于保存整个文本的信息 while ((temp = sr.ReadLine()) != null) { totalText += temp; totalText += "\r\n"; } textBoxIn.Text = totalText; textBoxIn.ForeColor = System.Drawing.Color.Blue; sr.Close(); } } }
为方便数据库操作而写的类如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace Finace { /// <summary> /// 功能:方便数据库操作(注册) /// 时间:2013/9/15 /// 作者:余颜凯 /// </summary> class DBOperate { private static string connString = "Data Source =(local);Initial Catalog =FinanceProject; User Id =sa ; Password =merlin "; public static SqlConnection connection = new SqlConnection(connString); } }
关于数据库上还要建立3个表:
表1:Consume:
序号 |
列名 |
类型 |
长度 |
小数位 |
标识 |
主键 |
允许空 |
默认值 |
说明 |
1 |
Id |
int |
4 |
0 |
是 |
是 |
否 |
无 |
|
2 |
ConsumeDate |
datetime |
8 |
3 |
否 |
||||
3 |
Type |
char |
10 |
0 |
否 |
||||
4 |
Catagory |
nvarchar |
20 |
0 |
否 |
||||
5 |
Money |
money |
8 |
4 |
否 |
||||
6 |
Description |
nvarchar |
200 |
0 |
是 |
表2:SavePasswords
序号 |
列名 |
类型 |
长度 |
小数位 |
标识 |
主键 |
允许空 |
默认值 |
说明 |
1 |
Id |
int |
4 |
是 |
否 |
||||
2 |
UserName |
nvarchar |
50 |
否 |
|||||
3 |
UserPassword |
nvarchar |
50 |
否 |
表3:Users
序号 |
列名 |
类型 |
长度 |
小数位 |
标识 |
主键 |
允许空 |
默认值 |
说明 |
1 |
UserName |
nvarchar |
20 |
是 |
否 |
无 |
|||
2 |
UserPassword |
nvarchar |
50 |
否 |