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

📄 presssetting.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>
	/// PressSetting 的摘要说明。
	/// </summary>
	public class PressSetting : 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.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button5;
		private System.ComponentModel.IContainer components;

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

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

			//ListView
			listView1.Columns.Add("编号",60,HorizontalAlignment.Left);
			listView1.Columns.Add("ISBN",120,HorizontalAlignment.Left);
			listView1.Columns.Add("出版社名称",120,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.components = new System.ComponentModel.Container();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			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.button4 = new System.Windows.Forms.Button();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.button3 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.textBox2);
			this.groupBox1.Controls.Add(this.textBox1);
			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(336, 100);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "基本设置";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(97, 60);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(184, 21);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(97, 20);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(184, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(23, 62);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(66, 17);
			this.label2.TabIndex = 1;
			this.label2.Text = "出版社名称";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(23, 22);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(29, 17);
			this.label1.TabIndex = 0;
			this.label1.Text = "ISBN";
			// 
			// listView1
			// 
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(8, 120);
			this.listView1.MultiSelect = false;
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(336, 208);
			this.listView1.TabIndex = 1;
			this.toolTip1.SetToolTip(this.listView1, "单击项目显示详细信息,并且可以修改!");
			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(40, 344);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 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(101, 344);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(48, 23);
			this.button2.TabIndex = 3;
			this.button2.Text = "更改";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button4
			// 
			this.button4.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button4.Location = new System.Drawing.Point(284, 344);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(48, 23);
			this.button4.TabIndex = 5;
			this.button4.Text = "关闭";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button3.Location = new System.Drawing.Point(162, 344);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(48, 23);
			this.button3.TabIndex = 4;
			this.button3.Text = "保存";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button5
			// 
			this.button5.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button5.Location = new System.Drawing.Point(223, 344);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(48, 23);
			this.button5.TabIndex = 6;
			this.button5.Text = "删除";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// PressSetting
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(352, 376);
			this.ControlBox = false;
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button4);
			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.Name = "PressSetting";
			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.PressSetting_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// 初始化界面
		/// </summary>
		private void initializeScreen()
		{
			textBox1.ReadOnly = true;
			textBox2.ReadOnly = true;
			textBox1.Text = "";
			textBox2.Text = "";
			button1.Enabled = true;
			button2.Enabled = false;
			button3.Enabled = false;
			button5.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.GetString(2));
					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 PressSetting_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)
		{
			string str = "";
			if(textBox1.Text != "" && textBox2.Text != "")
			{
				if(isUpdate)
				{
					str = "select * from 出版社 where ISBN='" + textBox1.Text + "'";
					cmd.CommandText =str;
					try
					{
						dr = cmd.ExecuteReader();
						dr.Read();
						if(dr.HasRows)
						{
							if(dr!=null)dr.Close();
							MessageBox.Show("已经存在此出版社了!");
							return;
						}
						if(dr!=null)dr.Close();
					}
					catch(Exception ex)
					{
						if(dr!=null)dr.Close();
						MessageBox.Show(ex.ToString());
						return;
					}
					str = "update 出版社 set ISBN='" + textBox1.Text + "',出版社名称='" + textBox2.Text + "' where ISBN='" + listView1.SelectedItems[0].SubItems[1].Text + "'";
					cmd.CommandText = str;
					try
					{
						cmd.ExecuteNonQuery();
						MessageBox.Show("更新成功!");
						initializeScreen();
					}
					catch(Exception ex)
					{
						MessageBox.Show(ex.ToString());
						return;
					}
				}
				else
				{
					str = "select * from 出版社 where ISBN='" + textBox1.Text + "'";
					cmd.CommandText =str;
					try
					{
						dr = cmd.ExecuteReader();
						dr.Read();
						if(dr.HasRows)
						{
							if(dr!=null)dr.Close();
							MessageBox.Show("已经存在此出版社了!");
							return;
						}
						if(dr!=null)dr.Close();
					}
					catch(Exception ex)
					{
						if(dr!=null)dr.Close();
						MessageBox.Show(ex.ToString());
						return;
					}
					str = "insert into 出版社(ISBN,出版社名称) values('" + textBox1.Text + "','" + textBox2.Text + "')";
					cmd.CommandText = str;
					try
					{
						cmd.ExecuteNonQuery();
						MessageBox.Show("添加成功!");
						initializeScreen();
					}
					catch(Exception ex)
					{
						MessageBox.Show(ex.ToString());
						return;
					}
				}
			}
			else
			{
				MessageBox.Show("记录项不能为空!");
				textBox1.Focus();
			}
		}
		/// <summary>
		/// 关闭按钮
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button4_Click(object sender, System.EventArgs e)
		{
			Global.sbpGlobal.Text = "就绪";
			this.Close();
		}
		/// <summary>
		/// 列表框单击事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listView1_Click(object sender, System.EventArgs e)
		{
			button1.Enabled = false;
			button2.Enabled = true;
			button5.Enabled = true;
			textBox1.Text = listView1.SelectedItems[0].SubItems[1].Text;
			textBox2.Text = listView1.SelectedItems[0].SubItems[2].Text;
		}
		/// <summary>
		/// 添加出版社
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e)
		{
			isUpdate = false;
			button1.Enabled = false;
			button2.Enabled = false;
			button3.Enabled = true;
			button5.Enabled = false;
			textBox1.Text = "";
			textBox1.ReadOnly = false;
			textBox2.Text = "";
			textBox2.ReadOnly = false;
			textBox1.Focus();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			isUpdate = true;
			textBox1.ReadOnly = false;
			textBox2.ReadOnly = false;
			button2.Enabled = false;
			button3.Enabled = true;
			button5.Enabled = false;
			button1.Enabled = false;
		}
		/// <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!="")
			{
				string str = "delete from 出版社 where ISBN='" + listView1.SelectedItems[0].SubItems[1].Text + "'";
				cmd.CommandText = str;
				try
				{
					cmd.ExecuteNonQuery();
					MessageBox.Show("删除成功!");
					initializeScreen();
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.ToString());
					return;
				}
			}
		}
	}
}

⌨️ 快捷键说明

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