Windows 窗体应用程序——图书管理系统总结(不带数据库操作

Windows 窗体应用程序——图书管理系统总结(不带数据库操作)

图书管理系统简介

实现功能:在windows Form平台上实现图书的添加、修改、删除功能

附图1:

图书管理系统主界面:

 

附图2:

图书管理系统修改图书界面:

 

功能实现:

分析图书管理系统主界面

对象分析:

Models 实例类

1、 图书类。图书对象的属性:图书编号、图书名称、图书价格、出版社、出版日期

2、 出版社类。用来存储出版社下拉框对象。出版社类的属性:出版社编号、出版社名称

功能分析:

图书管理系统主界面:实现按钮功能:

1、 添加图书

步骤分析:用户在控件中输入内容,点击按钮“添加图书”后。数据同步显示在下面的DataGridView控件中。

2、 修改图书  

步骤分析:用户在DataGridView中选中一条记录,单击按钮“修改图书”后,跳转到图书管理系统修改界面。在修改界面中展示要修改的图书对象。修改完成之后。点击按钮“提交”后,返回主界面显示修改完的内容。

3、 删除图书

步骤分析:用户在DataGridView中选中一条记录,单击按钮“删除图书”,弹出是否删除提示信息。确认删除后,该记录在DataGridView中移除

图书管理系统修改界面:实现按钮功能:

1、 提交修改

步骤分析:用户点击“提交”按钮后,修改后的图书内容返回主界面显示

具体实现

准备工作:制作两个窗体。主界面窗体(FrmMainBook.cs)修改界面窗体(FrmEditBook.cs)

注意

1、时间日期控件。修改属性:Format: 默认是Long 格式:2018年3月31日需要改为 short 格式:2018/3/31

2、 下拉框控件显示出版社名称实际存储出版社编号。具体参照下面出版社下拉框初始化方法

3、 DataGridView控件。添加中文显示列。并且修改列的属性与图书对象属性对应。绑定数据源为图书的属性,详见附图3

附图3

 

 

一、添加图书功能

步骤一:构造函数初始化

初始化内容:

1、 出版社下拉框初始化

2、 DataGridView自动生成列属性修改

  public FrmMainBook()

        {

            InitializeComponent();

            //初始化出版社

            InitPublisher();

            this.dgvBookList.AutoGenerateColumns = false;//不允许 DataGridView自动生成列

}

//初始化出版社

private void  InitPublisher()

        {

            //封装出版社对象,运用集合初始化器和对象初始化器赋予出版社下拉框固定值

            List<Publisher> publisherList = new List<Publisher>()

            {

                new Publisher() {PublisherId=1,PublisherName="北京出版社" },

                new Publisher() {PublisherId=2,PublisherName="上海出版社" },

                new Publisher() {PublisherId=3,PublisherName="南京出版社" },

                new Publisher() {PublisherId=4,PublisherName="山东出版社" },

                new Publisher() {PublisherId=5,PublisherName="天津出版社" },

            };

            //设置数据源

            this.cboxPubliserId.DataSource = publisherList;

            this.cboxPubliserId.DisplayMember = "PublisherName"; //设置下拉框显示名称

            this.cboxPubliserId.ValueMember = "PublisherId";  //设置下拉框提交值成员

            this.cboxPubliserId.SelectedIndex = -1; //下拉框开始显示空

        

 

        }

步骤二;添加图书按钮

private void btnAdd_Click(object sender, EventArgs e)

        {

            // [1] 检查数据、验证数据

            if(txtBarCode.Text.Trim()==" " || txtBarCode.TextLength == 0)

            {

                MessageBox.Show("图书编码不能为空!", "提示");

                txtBarCode.Focus();//将光标移到图书编码控件

                return;

            }

            // [2] 封装对象(将用户输入的内容转换为对象)

            Book book = new Book()

            {

                BarCode = this.txtBarCode.Text,

                BookName = this.txtBookName.Text,

                Price = Convert.ToDouble(this.txtPrice.Text),

                PublisherId =Convert.ToInt32(cboxPubliserId.SelectedValue),//注意获取下拉框控件的值

                PublishDate=Convert.ToDateTime(this.dtpPublishDate.Text)

            };

            // [3] 将对象保存到数据源(多数情况是数据库、也可以是其他数据源)

            // [4] 显示数据

           

            bookList.Add(book);

            //设置数据源

            this.dgvBookList.DataSource = null;

            this.dgvBookList.DataSource = bookList;

 

            //清空数据

            this.txtBarCode.Clear();

            this.txtBookName.Clear();

            this.txtPrice.Clear();

            this.cboxPubliserId.SelectedIndex = -1;

            this.txtBarCode.Focus();

        }

二、修改图书

主界面(FrmMainBook.cs):

private void btnEdit_Click(object sender, EventArgs e)

        {

           

            //[1]验证 如果当前dgv没有数据,则不执行任何操作

            if (this.dgvBookList.CurrentRow ==null || this.dgvBookList.RowCount == 0)

            {

                MessageBox.Show("请选择有效行", "提示");

                return;

            }

            //[2]获取当前修改的对象,首先找到图书条码,然后在dgv中找到当前对象

            String barCode = this.dgvBookList.CurrentRow.Cells["BarCode"].Value.ToString();

            Book objBook = (from b in bookList where b.BarCode.ToString().Equals(barCode) select b).First<Book>();

            //[3]显示要修改的窗体 把对象传到修改窗体中,这里不能用show方法要用ShowDialog()方法。注释:用Show()方法的话点击关闭也会继续执行

            FrmEditBook edit = new FrmEditBook(objBook);

            DialogResult result= edit.ShowDialog();

 

            //[4]根据修改是成功来判定是否显示

            if (result == DialogResult.OK)

            {

                //首先获取修改后的对象

                Book book =(Book) edit.Tag;

                //重新绑定当前行的值

                Book editBook = (from b in bookList where b.BarCode.ToString().Equals(barCode) select b).First<Book>();

                editBook.BarCode = book.BarCode;

                editBook.BookName = book.BookName;

                editBook.Price = book.Price;

                editBook.PublishDate = book.PublishDate;

                editBook.PublisherId = book.PublisherId;

                //刷新修改窗体

                this.dgvBookList.Refresh();

 

            }

         

        }

修改窗体(FrmEditBook.cs)

public partial class FrmEditBook : Form

    {

        public FrmEditBook()

        {

            InitializeComponent();

           

        }

        /// <summary>

        /// 构造方法传入主界面参数,并显示要修改的图书信息

        /// </summary>

        public FrmEditBook(Book objBook)

        {

            InitializeComponent();

            //初始化出版社下拉框

            InitPublisher();

            //显示图书信息。把传过来的图书对象转化到控件中

            this.txtBarCode.Text= objBook.BarCode;

            this.txtBookName.Text = objBook.BookName;

            this.txtPrice.Text = objBook.Price.ToString();

            this.dtpPublishDate.Text = objBook.PublishDate.ToShortDateString();//时间要用ToShortDateString()

            //给下拉框赋值

            this.cboxPubliserId.SelectedValue = objBook.PublisherId;

 

 

        }

 

        /// <summary>

        /// 初始化出版社

        /// </summary>

        private void InitPublisher()

        {

            List<Publisher> publisherList = new List<Publisher>()

            {

                new Publisher() {PublisherId=1,PublisherName="北京出版社" },

                new Publisher() {PublisherId=2,PublisherName="上海出版社" },

                new Publisher() {PublisherId=3,PublisherName="南京出版社" },

                new Publisher() {PublisherId=4,PublisherName="山东出版社" },

                new Publisher() {PublisherId=5,PublisherName="天津出版社" },

            };

            //设置数据源

            this.cboxPubliserId.DataSource = publisherList;

            this.cboxPubliserId.DisplayMember = "PublisherName"; //设置下拉框显示名称

            this.cboxPubliserId.ValueMember = "PublisherId";  //设置下拉框提交值成员

            //this.cboxPubliserId.SelectedIndex = -1;

 

 

        }

       

       

        /// <summary>

        /// 提交修改并在主界面中展示修改好的内容

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnCommit_Click(object sender, EventArgs e)

        {

            //提交修改 与添加的方法类似

            // [1] 检查数据、验证数据

            if (txtBarCode.Text.Trim() == " " || txtBarCode.TextLength == 0)

            {

                MessageBox.Show("图书编码不能为空!", "提示");

                txtBarCode.Focus();

                return;

            }

            // [2] 封装对象(将修改的内容装换为对象)

            Book book = new Book()

            {

                BarCode = this.txtBarCode.Text,

                BookName = this.txtBookName.Text,

                Price = Convert.ToDouble(this.txtPrice.Text),

                PublisherId = Convert.ToInt32(cboxPubliserId.SelectedValue),

                PublishDate = Convert.ToDateTime(this.dtpPublishDate.Text)

            };

            // [3] 将对象保存到数据源(多数情况是数据库、也可以是其他数据源)

 

            // [4] 保存修改的对象

            this.Tag = book;

            this.DialogResult = DialogResult.OK;

            this.Close();

 

        }

}

 

三、 删除图书对象

private void btnDelete_Click(object sender, EventArgs e)

        {

            //[1]验证删除 提示是否确认删除;如果当前没有任何数据,则不执行操作

            if (this.dgvBookList.RowCount == 0 || this.dgvBookList.CurrentRow == null)

            {

                MessageBox.Show("没有选中删除对象", "提示");

                return;

            }

           // 删除确认

           

                DialogResult result = MessageBox.Show("确认删除?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (result == DialogResult.Cancel)

                {

                    return;

                }

            //[2]获取当前图书对象的唯一标识图书编码然后找到该对象

            String barCode = this.dgvBookList.CurrentRow.Cells["BarCode"].Value.ToString();

            Book delBook = (from b in bookList where b.BarCode.ToString().Equals(barCode) select b).First<Book>();

            //[3]删除该对象用remove

            this.bookList.Remove(delBook);

            //[4]同步更新显示 bookList添加到dgv数据源,先清空数据源

            this.dgvBookList.DataSource = null;

            this.dgvBookList.DataSource = bookList;

        }

原文地址:https://www.cnblogs.com/cuig/p/8711808.html