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

📄 readertype.cs

📁 图书馆信息管理系统 Library Information Management System
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Library
{
	/// <summary>
	/// ReaderType 的摘要说明。
	/// </summary>
	public class ReaderType : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		private SqlCommand cmd ;
		private SqlDataReader dr;
		private bool isUpdate = false;

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

			listView1.Columns.Add("类型编号",70,HorizontalAlignment.Left);
			listView1.Columns.Add("类型名称",70,HorizontalAlignment.Left);
			listView1.Columns.Add("借阅天数",70,HorizontalAlignment.Left);
			listView1.Columns.Add("借阅量",60,HorizontalAlignment.Left);
			listView1.Columns.Add("超期罚款额(元/天)",130,HorizontalAlignment.Left);
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.listView1 = new System.Windows.Forms.ListView();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.textBox4);
			this.groupBox1.Controls.Add(this.textBox3);
			this.groupBox1.Controls.Add(this.textBox2);
			this.groupBox1.Controls.Add(this.textBox1);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(424, 100);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "基本设置";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(283, 62);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(112, 21);
			this.textBox4.TabIndex = 3;
			this.textBox4.Text = "";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(93, 62);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(118, 21);
			this.textBox3.TabIndex = 2;
			this.textBox3.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(283, 22);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(112, 21);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(93, 22);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(118, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(219, 64);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(54, 17);
			this.label4.TabIndex = 3;
			this.label4.Text = "超期罚款";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(29, 64);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(42, 17);
			this.label3.TabIndex = 2;
			this.label3.Text = "借阅量";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(219, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(54, 17);
			this.label2.TabIndex = 1;
			this.label2.Text = "借阅天数";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(29, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(54, 17);
			this.label1.TabIndex = 0;
			this.label1.Text = "类型名称";
			// 
			// listView1
			// 
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(8, 112);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(424, 224);
			this.listView1.TabIndex = 1;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.Click += new System.EventHandler(this.listView1_Click);
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button1.Location = new System.Drawing.Point(160, 352);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(56, 23);
			this.button1.TabIndex = 2;
			this.button1.Text = "新增";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button2.Location = new System.Drawing.Point(226, 352);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(56, 23);
			this.button2.TabIndex = 3;
			this.button2.Text = "更改";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button3.Location = new System.Drawing.Point(366, 352);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(56, 23);
			this.button3.TabIndex = 4;
			this.button3.Text = "关闭";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button4.Location = new System.Drawing.Point(292, 352);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(64, 23);
			this.button4.TabIndex = 5;
			this.button4.Text = "保存";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// ReaderType
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(442, 392);
			this.ControlBox = false;
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "ReaderType";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "读者类型";
			this.Load += new System.EventHandler(this.ReaderType_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// 初始化界面
		/// </summary>
		private void initializeScreen()
		{
			textBox1.Text = "";
			textBox1.ReadOnly = true;
			textBox2.Text = "";
			textBox2.ReadOnly = true;
			textBox3.Text = "";
			textBox3.ReadOnly = true;
			textBox4.Text = "";
			textBox4.ReadOnly = true;
			button2.Enabled = false;
			button4.Enabled = false;
			listView1.Items.Clear();
			ListViewItem item = null;
			string str = "select * from 读者类型";
			cmd.CommandText = str;
			try
			{
				dr = cmd.ExecuteReader();
				while(dr.Read())
				{
					item = new ListViewItem(dr.GetInt32(0).ToString());
					item.SubItems.Add(dr.GetString(1));
					item.SubItems.Add(dr.GetInt32(2).ToString());
					item.SubItems.Add(dr.GetInt32(3).ToString());
					item.SubItems.Add(dr.GetValue(4).ToString());
					listView1.Items.Add(item);
				}
				dr.Close();
			}
			catch(Exception ex)
			{
				if(dr != null) dr.Close();
				MessageBox.Show(ex.ToString());
			}
		}
		/// <summary>
		/// 窗体加载代码
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ReaderType_Load(object sender, System.EventArgs e)
		{
			cmd = Global.conn.CreateCommand();
			initializeScreen();
		}
		/// <summary>
		/// 关闭按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button3_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e)
		{
			isUpdate = false;
			textBox1.Text = "";
			textBox2.Text = "";
			textBox3.Text = "";
			textBox4.Text = "";
			textBox1.ReadOnly = false;
			textBox2.ReadOnly = false;
			textBox3.ReadOnly = false;
			textBox4.ReadOnly = false;
			button1.Enabled = false;
			button2.Enabled = false;
			button4.Enabled = true;
			textBox1.Focus();
		}
		/// <summary>
		/// 更改
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button2_Click(object sender, System.EventArgs e)
		{
			isUpdate = true;
			textBox1.ReadOnly = false;
			textBox2.ReadOnly = false;
			textBox3.ReadOnly = false;
			textBox4.ReadOnly = false;
			button1.Enabled = false;
			button4.Enabled = true;
		}
		/// <summary>
		/// ListView项目单击事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listView1_Click(object sender, System.EventArgs e)
		{
			if(listView1.SelectedItems[0].SubItems[0].Text!="")
			{
				textBox1.ReadOnly = true;
				textBox2.ReadOnly = true;
				textBox3.ReadOnly = true;
				textBox4.ReadOnly = true;
				button1.Enabled = true;
				button2.Enabled = true;
				button4.Enabled = false;
				textBox1.Text = listView1.SelectedItems[0].SubItems[1].Text;
				textBox2.Text = listView1.SelectedItems[0].SubItems[2].Text;
				textBox3.Text = listView1.SelectedItems[0].SubItems[3].Text;
				textBox4.Text = listView1.SelectedItems[0].SubItems[4].Text;
			}
			else
			{
				button2.Enabled = false;
			}
		}
		/// <summary>
		/// 保存记录
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button4_Click(object sender, System.EventArgs e)
		{
			string str = "select * from 读者类型 where 类型名称='" + textBox1.Text  + "'";
			cmd.CommandText = str;
			try
			{
				dr = cmd.ExecuteReader();
				dr.Read();
				if(dr.HasRows)
				{
					if(dr!=null)dr.Close();
					MessageBox.Show("类型已经存在!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				}
				if(dr!=null)dr.Close();
			}
			catch(Exception ex)
			{
				if(dr!=null)dr.Close();
				MessageBox.Show("发生如下错误:\n\t" + ex.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
				return;
			}
			if(isUpdate)
			{
				str = "update 读者类型 set 类型名称='" + textBox1.Text + "',借阅天数=" + Convert.ToInt32(textBox2.Text) + "," +
					"借阅量=" + Convert.ToInt32(textBox3.Text) + ",超期罚款额=" + Convert.ToDecimal(textBox4.Text) + " where 类型名称='" + textBox1.Text + "'";
			}
			else
			{
				str = "insert into 读者类型 values('" + textBox1.Text + "'," + Convert.ToInt32(textBox2.Text) + "," + Convert.ToInt32(textBox3.Text) + "," + Convert.ToDecimal(textBox4.Text) + ")";
			}
			cmd.CommandText = str;
			try
			{
				cmd.ExecuteNonQuery();
				MessageBox.Show("操作成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				initializeScreen();
			}
			catch(Exception ex)
			{
				MessageBox.Show("发生如下错误:\n\t" + ex.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
		/// <summary>
		/// 删除记录
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button5_Click(object sender, System.EventArgs e)
		{
			if(listView1.SelectedItems[0].SubItems[0].Text!="")
			{
				if(MessageBox.Show("此操作会造成所有与之相关的记录被删除!是否继续?","询问",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
				{
					string str = "delete * from 读者类型 where 类型名称='" + textBox1.Text + "'";
				}
			}
		}
	}
}

⌨️ 快捷键说明

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