⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 wince开发的订购系统
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Reflection;
using System.Xml;
using System.ComponentModel;

namespace Book_Admin
{
	/// <summary>
	/// MainForm 的摘要说明。
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.TabPage tabPage3;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuFile;
		private System.Windows.Forms.MenuItem menuNewDataFile;
		private System.Windows.Forms.MenuItem menuOpenDataFile;
		private System.Windows.Forms.MenuItem menuExit;
		private System.Windows.Forms.MenuItem menuHelp;
		private System.Windows.Forms.MenuItem menuHelpContent;
		private System.Windows.Forms.MenuItem menuAbout;
		private System.Windows.Forms.MenuItem menuSaveDataFile;

		private static string DataDirectory = @"\My Documents\My Library";
		private bool ChangedDataSaved =  true;
		private DataSet dsBookAdmin;
		private DataTable dtBooks;
		private System.Windows.Forms.Button btnEmptyBook;
		private System.Windows.Forms.Button btnRemoveBook;
		private System.Windows.Forms.Button btnEditBook;
		private System.Windows.Forms.ListView BookList;
		private System.Windows.Forms.Button btnAddBook;

		public MainForm()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuFile = new System.Windows.Forms.MenuItem();
			this.menuNewDataFile = new System.Windows.Forms.MenuItem();
			this.menuOpenDataFile = new System.Windows.Forms.MenuItem();
			this.menuSaveDataFile = new System.Windows.Forms.MenuItem();
			this.menuExit = new System.Windows.Forms.MenuItem();
			this.menuHelp = new System.Windows.Forms.MenuItem();
			this.menuHelpContent = new System.Windows.Forms.MenuItem();
			this.menuAbout = new System.Windows.Forms.MenuItem();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.btnEmptyBook = new System.Windows.Forms.Button();
			this.btnRemoveBook = new System.Windows.Forms.Button();
			this.btnAddBook = new System.Windows.Forms.Button();
			this.BookList = new System.Windows.Forms.ListView();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.tabPage3 = new System.Windows.Forms.TabPage();
			this.btnEditBook = new System.Windows.Forms.Button();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.menuFile);
			this.mainMenu1.MenuItems.Add(this.menuHelp);
			// 
			// menuFile
			// 
			this.menuFile.MenuItems.Add(this.menuNewDataFile);
			this.menuFile.MenuItems.Add(this.menuOpenDataFile);
			this.menuFile.MenuItems.Add(this.menuSaveDataFile);
			this.menuFile.MenuItems.Add(this.menuExit);
			this.menuFile.Text = "文件";
			// 
			// menuNewDataFile
			// 
			this.menuNewDataFile.Text = "新建数据文件...";
			this.menuNewDataFile.Click += new System.EventHandler(this.menuNewDataFile_Click);
			// 
			// menuOpenDataFile
			// 
			this.menuOpenDataFile.Text = "打开数据文件...";
			this.menuOpenDataFile.Click += new System.EventHandler(this.menuOpenDataFile_Click);
			// 
			// menuSaveDataFile
			// 
			this.menuSaveDataFile.Text = "存储数据文件...";
			this.menuSaveDataFile.Click += new System.EventHandler(this.menuSaveDataFile_Click);
			// 
			// menuExit
			// 
			this.menuExit.Text = "退出";
			this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
			// 
			// menuHelp
			// 
			this.menuHelp.MenuItems.Add(this.menuHelpContent);
			this.menuHelp.MenuItems.Add(this.menuAbout);
			this.menuHelp.Text = "帮助";
			// 
			// menuHelpContent
			// 
			this.menuHelpContent.Text = "帮助信息...";
			// 
			// menuAbout
			// 
			this.menuAbout.Text = "关于...";
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Controls.Add(this.tabPage3);
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(250, 272);
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.btnEditBook);
			this.tabPage1.Controls.Add(this.btnEmptyBook);
			this.tabPage1.Controls.Add(this.btnRemoveBook);
			this.tabPage1.Controls.Add(this.btnAddBook);
			this.tabPage1.Controls.Add(this.BookList);
			this.tabPage1.Location = new System.Drawing.Point(4, 4);
			this.tabPage1.Size = new System.Drawing.Size(242, 247);
			this.tabPage1.Text = "图书管理";
			// 
			// btnEmptyBook
			// 
			this.btnEmptyBook.Location = new System.Drawing.Point(184, 208);
			this.btnEmptyBook.Size = new System.Drawing.Size(48, 24);
			this.btnEmptyBook.Text = "清空";
			this.btnEmptyBook.Click += new System.EventHandler(this.btnEmptyBook_Click);
			// 
			// btnRemoveBook
			// 
			this.btnRemoveBook.Location = new System.Drawing.Point(128, 208);
			this.btnRemoveBook.Size = new System.Drawing.Size(48, 24);
			this.btnRemoveBook.Text = "删除";
			this.btnRemoveBook.Click += new System.EventHandler(this.btnRemoveBook_Click);
			// 
			// btnAddBook
			// 
			this.btnAddBook.Location = new System.Drawing.Point(8, 208);
			this.btnAddBook.Size = new System.Drawing.Size(48, 24);
			this.btnAddBook.Text = "增加";
			this.btnAddBook.Click += new System.EventHandler(this.btnAddBook_Click);
			// 
			// BookList
			// 
			this.BookList.Location = new System.Drawing.Point(8, 8);
			this.BookList.Size = new System.Drawing.Size(225, 192);
			// 
			// tabPage2
			// 
			this.tabPage2.Location = new System.Drawing.Point(4, 4);
			this.tabPage2.Size = new System.Drawing.Size(242, 247);
			this.tabPage2.Text = "读者管理";
			// 
			// tabPage3
			// 
			this.tabPage3.Location = new System.Drawing.Point(4, 4);
			this.tabPage3.Size = new System.Drawing.Size(242, 247);
			this.tabPage3.Text = "借阅管理";
			// 
			// btnEditBook
			// 
			this.btnEditBook.Location = new System.Drawing.Point(64, 208);
			this.btnEditBook.Size = new System.Drawing.Size(48, 24);
			this.btnEditBook.Text = "编辑";
			this.btnEditBook.Click += new System.EventHandler(this.btnEditBook_Click);
			// 
			// MainForm
			// 
			this.ClientSize = new System.Drawing.Size(250, 280);
			this.Controls.Add(this.tabControl1);
			this.Menu = this.mainMenu1;
			this.Text = "简易图书管理";
			this.Load += new System.EventHandler(this.MainForm_Load);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>

		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			LogonForm LogonFrm = new LogonForm();

			if (LogonFrm.ShowDialog() == DialogResult.Cancel)
			{
				LogonFrm.Dispose();
				this.Close();
			}
			else
			{
				this.Text+= " - Untitle";
				LogonFrm.Dispose();
				//OpenCatalogFile(DataFile);
			}

			//Directory of XML data files
			Directory.CreateDirectory(DataDirectory);

			dsBookAdmin = new DataSet();
			dtBooks = new DataTable();
			dsBookAdmin.Tables.Add(dtBooks);
			dtBooks.TableName = "Book";
			dtBooks.Columns.Add("title");
			dtBooks.Columns.Add("author");
			dtBooks.Columns.Add("publisher");
			dtBooks.Columns.Add("publishing_date");
			dtBooks.Columns.Add("price");
			dtBooks.Columns.Add("contentType");
			dtBooks.Columns.Add("format");
			dtBooks.Columns.Add("isbn");

			BookList.View = View.Details;
			BookList.FullRowSelect = true;
			BookList.CheckBoxes = true;
			BookList.Columns.Add("书名",-1, HorizontalAlignment.Center);
			BookList.Columns.Add("作者",-1, HorizontalAlignment.Center);
			BookList.Columns.Add("出版社",-1, HorizontalAlignment.Center);
			BookList.Columns.Add("出版日期",-1, HorizontalAlignment.Center);
			BookList.Columns.Add("价格",-1, HorizontalAlignment.Center);
			BookList.Columns.Add("图书类型",-1, HorizontalAlignment.Center);
			BookList.Columns.Add("装帧类型",-1, HorizontalAlignment.Center);
			BookList.Columns.Add("书号",-1, HorizontalAlignment.Center);
		}

		private void AddDataToBookListView(DataRow row)
		{
			ListViewItem item;

			item = new ListViewItem(row["title"].ToString());
			item.SubItems.Add(row["author"].ToString());
			item.SubItems.Add(row["publisher"].ToString());
			item.SubItems.Add(row["publishing_date"].ToString());
			item.SubItems.Add(row["price"].ToString());
			item.SubItems.Add(row["contentType"].ToString());
			item.SubItems.Add(row["format"].ToString());
			item.SubItems.Add(row["isbn"].ToString());

			BookList.Items.Add(item);
			ChangedDataSaved = false;  //A flag to remind the user when exit.
		}

		private void RenewBookListView()
		{
			ListViewItem item;

			BookList.Items.Clear();
			foreach(DataRow row in dtBooks.Rows)
			{
				item = new ListViewItem(row["title"].ToString());
				item.SubItems.Add(row["author"].ToString());
				item.SubItems.Add(row["publisher"].ToString());
				item.SubItems.Add(row["publishing_date"].ToString());
				item.SubItems.Add(row["price"].ToString());
				item.SubItems.Add(row["contentType"].ToString());
				item.SubItems.Add(row["format"].ToString());
				item.SubItems.Add(row["isbn"].ToString());

				BookList.Items.Add(item);
			}
		}

		private void SaveDataFile(string DataFileName)
		{
			FileInfo BookDataFile;
			XmlWriter Writer;

			Cursor.Current=Cursors.WaitCursor;

			//Check to see if the file already exists, and if so delete it. 
			BookDataFile = new FileInfo(DataDirectory + @"\" + DataFileName);
			if (BookDataFile.Exists) 
			{
				BookDataFile.Delete();
			}

			Writer = new XmlTextWriter(DataFileName, System.Text.Encoding.Unicode);
			dsBookAdmin.WriteXml(Writer, XmlWriteMode.WriteSchema);
			Writer.Close();
			ChangedDataSaved = true;
		
			Cursor.Current=Cursors.Default;
		}

		private void menuNewDataFile_Click(object sender, System.EventArgs e)
		{
			dtBooks.Rows.Clear();
			BookList.Items.Clear();
			ChangedDataSaved = true;  //An empty DataSet needn't to be saved.
		}

		private void menuExit_Click(object sender, System.EventArgs e)
		{
			if(!ChangedDataSaved) 
			{
				switch(MessageBox.Show("数据已经改变,是否存盘?", "注意", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
				{
					case DialogResult.Yes:
						menuSaveDataFile_Click(this, new System.EventArgs());
						break;
					case DialogResult.No:
						//do nothing;
						break;
					case DialogResult.Cancel:
						//give up exiting
						return;
				}
			}
			this.Dispose();
			this.Close();
		}

		private void menuOpenDataFile_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "XML files | *.xml";
			dlg.InitialDirectory = DataDirectory;

			if (dlg.ShowDialog() == DialogResult.OK)
			{
				dsBookAdmin.ReadXml(dlg.FileName);
				dtBooks = dsBookAdmin.Tables["Book"];
				RenewBookListView();
			}
		}

		private void menuSaveDataFile_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.Filter = "XML File|*.xml";
			dlg.FileName = "BookDataFile.xml";
			dlg.InitialDirectory = DataDirectory;

			if (dlg.ShowDialog() == DialogResult.OK)
			{
				SaveDataFile(dlg.FileName);
			}
		}

		private void btnAddBook_Click(object sender, System.EventArgs e)
		{
			AddbookForm addbookFrm = new AddbookForm();
			if (addbookFrm.ShowDialog() == DialogResult.Cancel)
			{
				addbookFrm.Dispose();
			}
			else
			{
				DataRow row = dtBooks.NewRow();

				row["title"] = addbookFrm.textBox1.Text;
				row["author"] = addbookFrm.textBox2.Text;
				row["publisher"] = addbookFrm.textBox3.Text;
				row["publishing_date"] = addbookFrm.numericUpDown1.Text + "-" + addbookFrm.numericUpDown2.Text;
				row["price"] = addbookFrm.textBox4.Text;
				row["contentType"] = addbookFrm.domainUpDown1.Text;
				row["format"] = addbookFrm.domainUpDown2.Text;
				row["isbn"] = addbookFrm.textBox5.Text;
				
				dtBooks.Rows.Add(row);
				AddDataToBookListView(row);
			}
		}

		private void btnEditBook_Click(object sender, System.EventArgs e)
		{
			if (BookList.SelectedIndices.Count > 0)
			{
				int SelectedRecord = Convert.ToInt16(BookList.SelectedIndices[0]);
				EditbookForm editbookFrm = new EditbookForm(dtBooks.Rows[SelectedRecord]);
				if (editbookFrm.ShowDialog() == DialogResult.OK)
				{

					dtBooks.Rows[SelectedRecord]["title"] = editbookFrm.textBox1.Text;
					dtBooks.Rows[SelectedRecord]["author"] = editbookFrm.textBox2.Text;
					dtBooks.Rows[SelectedRecord]["publisher"] = editbookFrm.textBox3.Text;
					dtBooks.Rows[SelectedRecord]["publishing_date"] = editbookFrm.numericUpDown1.Text + "-" + editbookFrm.numericUpDown2.Text;
					dtBooks.Rows[SelectedRecord]["price"] = editbookFrm.textBox4.Text;
					dtBooks.Rows[SelectedRecord]["contentType"] = editbookFrm.domainUpDown1.Text;
					dtBooks.Rows[SelectedRecord]["format"] = editbookFrm.domainUpDown2.Text;
					dtBooks.Rows[SelectedRecord]["isbn"] = editbookFrm.textBox5.Text;

					RenewBookListView();
					ChangedDataSaved = false;
				}
				editbookFrm.Dispose();
			}
		}

		private void btnRemoveBook_Click(object sender, System.EventArgs e)
		{
			int i = 0;

			while(i <  BookList.Items.Count)
			{
				if(BookList.Items[i].Checked)
				{
					BookList.Items.RemoveAt(i);
					dtBooks.Rows.RemoveAt(i);
					ChangedDataSaved = false;
				}
				i++;
			}
		}

		private void btnEmptyBook_Click(object sender, System.EventArgs e)
		{
			if(MessageBox.Show("此操作将删除所有图书,是否继续?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
			{
				dtBooks.Rows.Clear();
				BookList.Items.Clear();
				ChangedDataSaved = false;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -