📄 libraryinfo.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>
/// LibraryInfo 的摘要说明。
/// </summary>
public class LibraryInfo : 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.Label label5;
private System.Windows.Forms.Label label6;
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.TextBox textBox5;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.Button btnChange;
private System.Windows.Forms.Button btnClose;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private SqlCommand cmd ;
private SqlDataReader dr ;
private int id = 0;
private System.Windows.Forms.Button btnSave;
public LibraryInfo()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
}
/// <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.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.textBox5 = new System.Windows.Forms.TextBox();
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.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
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.btnChange = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.dateTimePicker1);
this.groupBox1.Controls.Add(this.textBox5);
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.label6);
this.groupBox1.Controls.Add(this.label5);
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(448, 208);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "图书馆基本信息";
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(280, 78);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.ShowUpDown = true;
this.dateTimePicker1.Size = new System.Drawing.Size(152, 21);
this.dateTimePicker1.TabIndex = 2;
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(88, 166);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(344, 21);
this.textBox5.TabIndex = 5;
this.textBox5.Text = "";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(280, 122);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(152, 21);
this.textBox4.TabIndex = 4;
this.textBox4.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(88, 122);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(112, 21);
this.textBox3.TabIndex = 3;
this.textBox3.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 78);
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(88, 34);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(16, 168);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(54, 17);
this.label6.TabIndex = 5;
this.label6.Text = "联系地址";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(216, 124);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(54, 17);
this.label5.TabIndex = 4;
this.label5.Text = "联系信箱";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(216, 80);
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(16, 124);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(54, 17);
this.label3.TabIndex = 2;
this.label3.Text = "联系电话";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(16, 80);
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(16, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(66, 17);
this.label1.TabIndex = 0;
this.label1.Text = "图书馆名称";
//
// btnChange
//
this.btnChange.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnChange.Location = new System.Drawing.Point(216, 232);
this.btnChange.Name = "btnChange";
this.btnChange.TabIndex = 6;
this.btnChange.Text = "更改";
this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
//
// btnClose
//
this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnClose.Location = new System.Drawing.Point(380, 232);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 8;
this.btnClose.Text = "关闭";
this.btnClose.Click += new System.EventHandler(this.button3_Click);
//
// btnSave
//
this.btnSave.Enabled = false;
this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnSave.Location = new System.Drawing.Point(298, 232);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 9;
this.btnSave.Text = "保存";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// LibraryInfo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(466, 272);
this.ControlBox = false;
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnChange);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "LibraryInfo";
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.LibraryInfo_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 初始化显示界面
/// </summary>
private void initializeScreen()
{
string str = "select * from 图书馆信息";
cmd.CommandText = str;
try
{
dr = cmd.ExecuteReader();
dr.Read();
if(dr.HasRows)
{
id = dr.GetInt32(0);
textBox1.Text = dr.GetString(1);
textBox1.ReadOnly = true;
textBox2.Text = dr.GetString(2);
textBox2.ReadOnly = true;
dateTimePicker1.Value = dr.GetDateTime(3);
dateTimePicker1.Enabled = false;
textBox3.Text = dr.GetString(4);
textBox3.ReadOnly = true;
textBox4.Text = dr.GetString(5);
textBox4.ReadOnly = true;
textBox5.Text = dr.GetString(6);
textBox5.ReadOnly = true;
}
else
{
id = 0;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
textBox4.ReadOnly = false;
textBox5.ReadOnly = false;
dateTimePicker1.Enabled = true;
}
}
catch(Exception ex)
{
if(dr !=null) dr.Close();
MessageBox.Show(ex.ToString());
}
finally
{
if(dr !=null) dr.Close();
}
}
/// <summary>
/// 初始化编辑控件
/// </summary>
/// <param name="canEdit"></param>
private void initializeTextBox(bool canEdit)
{
textBox1.ReadOnly = canEdit;
textBox2.ReadOnly = canEdit;
textBox3.ReadOnly = canEdit;
textBox4.ReadOnly = canEdit;
textBox5.ReadOnly = canEdit;
dateTimePicker1.Enabled = !canEdit;
}
/// <summary>
/// 关闭按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, System.EventArgs e)
{
this.Close();
}
/// <summary>
/// 检查数据
/// </summary>
private void CheckData()
{
if(textBox1.Text == "")
{
MessageBox.Show("图书馆名称不能为空!");
initializeTextBox(true);
textBox1.Focus();
}
if(textBox2.Text == "")
{
MessageBox.Show("图书馆馆长名称不能为空!");
initializeTextBox(true);
textBox2.Focus();
}
}
/// <summary>
/// 保存修改
/// </summary>
private void saveChanged()
{
string str;
if(id != 0)
{
str = "update 图书馆信息 set 图书馆名称='" + textBox1.Text + "'," +
"图书馆馆长='" + textBox2.Text + "',建馆日期='" + dateTimePicker1.Value.ToShortDateString() +
"',联系电话='" + textBox3.Text + "',联系信箱='" + textBox4.Text + "'," +
"图书馆地址='" + textBox5.Text + "' where ID=" + id;
cmd.CommandText = str;
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
else
{
str = "insert into 图书馆信息 values('" + textBox1.Text + "','" + textBox2.Text +
"','" + dateTimePicker1.Value.ToShortDateString() + "','" + textBox3.Text +
"','" + textBox4.Text + "','" + textBox5.Text + "')";
cmd.CommandText = str;
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
}
/// <summary>
/// 更改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnChange_Click(object sender, System.EventArgs e)
{
initializeTextBox(false);
btnSave.Enabled = true;
btnChange.Enabled = false;
}
/// <summary>
/// 窗体加载代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LibraryInfo_Load(object sender, System.EventArgs e)
{
cmd = Global.conn.CreateCommand();
initializeScreen();
}
/// <summary>
/// 保存按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, System.EventArgs e)
{
btnChange.Enabled = true;
btnSave.Enabled = false;
saveChanged();
initializeTextBox(true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -