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

📄 form1.cs

📁 c#标准教程适合与处于不同学习阶段的人
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using T = System.Diagnostics.Trace;
using SD=System.Data;              // general DB classes
using SQL=System.Data.SqlClient;   // Microsoft SQL Server databases
using SWF=System.Windows.Forms;

namespace BankCustomerApp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtFirstName;
		private System.Windows.Forms.TextBox txtLastName;
		private System.Windows.Forms.TextBox txtBalance;
		private System.Windows.Forms.ListBox lstCustomers;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtAmount;
		private System.Windows.Forms.Button cmdDeposit;
		private System.Windows.Forms.Button cmdWithdrawal;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnuFile;
		private System.Windows.Forms.MenuItem mnuFileFindByID;
		private System.Windows.Forms.MenuItem mnuFileDeleteSelected;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem mnuFileExit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lstCustomers = new System.Windows.Forms.ListBox();
			this.txtFirstName = new System.Windows.Forms.TextBox();
			this.txtLastName = new System.Windows.Forms.TextBox();
			this.txtBalance = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.txtAmount = new System.Windows.Forms.TextBox();
			this.cmdDeposit = new System.Windows.Forms.Button();
			this.cmdWithdrawal = new System.Windows.Forms.Button();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.mnuFile = new System.Windows.Forms.MenuItem();
			this.mnuFileFindByID = new System.Windows.Forms.MenuItem();
			this.mnuFileDeleteSelected = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.mnuFileExit = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// lstCustomers
			// 
			this.lstCustomers.ItemHeight = 24;
			this.lstCustomers.Location = new System.Drawing.Point(24, 24);
			this.lstCustomers.Name = "lstCustomers";
			this.lstCustomers.Size = new System.Drawing.Size(184, 316);
			this.lstCustomers.Sorted = true;
			this.lstCustomers.TabIndex = 0;
			this.lstCustomers.TabStop = false;
			this.lstCustomers.SelectedIndexChanged += new System.EventHandler(this.lstCustomers_SelectedIndexChanged);
			// 
			// txtFirstName
			// 
			this.txtFirstName.Location = new System.Drawing.Point(248, 32);
			this.txtFirstName.Name = "txtFirstName";
			this.txtFirstName.ReadOnly = true;
			this.txtFirstName.Size = new System.Drawing.Size(176, 29);
			this.txtFirstName.TabIndex = 1;
			this.txtFirstName.Text = "";
			// 
			// txtLastName
			// 
			this.txtLastName.Location = new System.Drawing.Point(248, 72);
			this.txtLastName.Name = "txtLastName";
			this.txtLastName.ReadOnly = true;
			this.txtLastName.Size = new System.Drawing.Size(176, 29);
			this.txtLastName.TabIndex = 2;
			this.txtLastName.Text = "";
			// 
			// txtBalance
			// 
			this.txtBalance.Location = new System.Drawing.Point(248, 112);
			this.txtBalance.Name = "txtBalance";
			this.txtBalance.ReadOnly = true;
			this.txtBalance.Size = new System.Drawing.Size(176, 29);
			this.txtBalance.TabIndex = 3;
			this.txtBalance.Text = "";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(248, 176);
			this.label1.Name = "label1";
			this.label1.TabIndex = 4;
			this.label1.Text = "amount:";
			// 
			// txtAmount
			// 
			this.txtAmount.Location = new System.Drawing.Point(248, 208);
			this.txtAmount.Name = "txtAmount";
			this.txtAmount.Size = new System.Drawing.Size(168, 29);
			this.txtAmount.TabIndex = 5;
			this.txtAmount.Text = "";
			// 
			// cmdDeposit
			// 
			this.cmdDeposit.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cmdDeposit.Location = new System.Drawing.Point(256, 256);
			this.cmdDeposit.Name = "cmdDeposit";
			this.cmdDeposit.Size = new System.Drawing.Size(152, 32);
			this.cmdDeposit.TabIndex = 6;
			this.cmdDeposit.Text = "Deposit";
			this.cmdDeposit.Click += new System.EventHandler(this.cmdDeposit_Click);
			// 
			// cmdWithdrawal
			// 
			this.cmdWithdrawal.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cmdWithdrawal.Location = new System.Drawing.Point(256, 304);
			this.cmdWithdrawal.Name = "cmdWithdrawal";
			this.cmdWithdrawal.Size = new System.Drawing.Size(152, 32);
			this.cmdWithdrawal.TabIndex = 7;
			this.cmdWithdrawal.Text = "Withdrawal";
			this.cmdWithdrawal.Click += new System.EventHandler(this.cmdWithdrawal_Click);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mnuFile});
			// 
			// mnuFile
			// 
			this.mnuFile.Index = 0;
			this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuFileDeleteSelected,
																					this.mnuFileFindByID,
																					this.menuItem4,
																					this.mnuFileExit});
			this.mnuFile.Text = "&File";
			// 
			// mnuFileFindByID
			// 
			this.mnuFileFindByID.Index = 1;
			this.mnuFileFindByID.Text = "&Find by ID...";
			this.mnuFileFindByID.Click += new System.EventHandler(this.mnuFileFindByID_Click);
			// 
			// mnuFileDeleteSelected
			// 
			this.mnuFileDeleteSelected.Index = 0;
			this.mnuFileDeleteSelected.Text = "&Delete selected";
			this.mnuFileDeleteSelected.Click += new System.EventHandler(this.mnuFileDeleteSelected_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 2;
			this.menuItem4.Text = "-";
			// 
			// mnuFileExit
			// 
			this.mnuFileExit.Index = 3;
			this.mnuFileExit.Text = "E&xit";
			this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(10, 22);
			this.ClientSize = new System.Drawing.Size(446, 367);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cmdWithdrawal,
																		  this.cmdDeposit,
																		  this.txtAmount,
																		  this.label1,
																		  this.txtBalance,
																		  this.txtLastName,
																		  this.txtFirstName,
																		  this.lstCustomers});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Bank Customer App";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void Form1_Load(object sender, System.EventArgs e)
		{
			SQL.SqlConnection  dbConn = null;
			SQL.SqlDataReader  dbReader = null;

			try
			{
				string  sConnect;
				sConnect = string.Format("Server={0};Database={1};{2}",
					"localhost", 
					"Sales",
					"Integrated Security=SSPI");  //id=sa;pwd=????				dbConn = new SQL.SqlConnection(sConnect);
				dbConn.Open();

				SWF.MessageBox.Show( dbConn.State.ToString() );

				string  sql;  BankCustomer  c;
				sql = "Select * From Customers Order By LastName Asc, FirstName Asc;";

				SQL.SqlCommand  dbCmd;
				dbCmd = new SQL.SqlCommand(sql, dbConn);

				dbReader = dbCmd.ExecuteReader();

				while (dbReader.Read()) // retrieve records 1-by-1...
				{
					c = new BankCustomer(dbReader["FirstName"].ToString(),
						dbReader["LastName"].ToString(),
						System.Convert.ToDecimal(dbReader["AcctBalance"]));

					Globals.Customers.Add(c);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally 
			{
				try{ dbReader.Close(); } 
				catch{}
				try{ dbConn.Close();   } 
				catch{}
			}

			SWF.MessageBox.Show( dbConn.State.ToString() );

			// bind customers to list box for display
			this.lstCustomers.DataSource = Globals.Customers;
			this.lstCustomers.SelectedIndex = -1;
		}

		private void lstCustomers_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			BankCustomer  bc;
			bc = (BankCustomer) this.lstCustomers.SelectedItem;
			if (bc == null)
				return;

			T.WriteLineIf(Globals.Trace, "User selected '" + bc.ToString() + ".");
			T.Flush();

			//
			// bind selected object to text boxes for access...
			//
			this.txtFirstName.DataBindings.Clear();
			this.txtLastName.DataBindings.Clear();
			this.txtBalance.DataBindings.Clear();

			this.txtFirstName.DataBindings.Add("Text", bc, "FirstName");
			this.txtLastName.DataBindings.Add("Text", bc, "LastName");
			this.txtBalance.DataBindings.Add("Text", bc, "FormattedBalance");
		}

		private void cmdDeposit_Click(object sender, System.EventArgs e)
		{
			BankCustomer  bc;
			bc = (BankCustomer) this.lstCustomers.SelectedItem;

			if (bc == null)
			{
				MessageBox.Show("Please select a customer first.");
				return;
			}

			decimal  amt;
			amt = decimal.Parse( this.txtAmount.Text );

			T.Indent();
			T.WriteLineIf(Globals.Trace, "About to deposit " + amt.ToString());
			T.Flush();

			bc.Deposit(amt);
			this.txtBalance.Text = bc.GetFormattedBalance();

			T.WriteLineIf(Globals.Trace, "New balance: " + bc.GetFormattedBalance());
			T.Flush();
			T.Unindent();

			MessageBox.Show("Deposited.");
		}

		private void cmdWithdrawal_Click(object sender, System.EventArgs e)
		{
			BankCustomer  bc;
			bc = (BankCustomer) this.lstCustomers.SelectedItem;

			if (bc == null)
			{
				MessageBox.Show("Please select a customer first.");
				return;
			}

			decimal  amt;
			amt = decimal.Parse( this.txtAmount.Text );

			T.Indent();
			T.WriteLineIf(Globals.Trace, "About to withdraw " + amt.ToString());
			T.Flush();

			bc.Withdraw(amt);
			this.txtBalance.Text = bc.GetFormattedBalance();

			T.WriteLineIf(Globals.Trace, "New balance: " + bc.GetFormattedBalance());
			T.Flush();
			T.Unindent();

			MessageBox.Show("Withdrawn.");
		}

		private void cmdFindByID_Click(object sender, System.EventArgs e)
		{
			// show form modally, then have .NET dispose for us...
			using (FormInputID frm = new FormInputID())
			{
				DialogResult  dr;
				int           id;

				dr = frm.ShowDialog();
				if (dr == DialogResult.OK)
				{
					id = frm.CustomerID;
					MessageBox.Show("Simulating search for customer " + id + "...", "Bank Customer App", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void mnuFileExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void mnuFileFindByID_Click(object sender, System.EventArgs e)
		{
			// show form modally, then have .NET dispose for us...
			using (FormInputID frm = new FormInputID())
			{
				DialogResult  dr;
				int           id;

				dr = frm.ShowDialog();
				if (dr == DialogResult.OK)
				{
					id = frm.CustomerID;
					MessageBox.Show("Simulating search for customer " + id + "...", "Bank Customer App", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void mnuFileDeleteSelected_Click(object sender, System.EventArgs e)
		{
			BankCustomer  bc;
			bc = (BankCustomer) this.lstCustomers.SelectedItem;
			if (bc == null)
			{
				MessageBox.Show("Please select a customer to delete...");
				return;
			}

			SQL.SqlConnection  dbConn = null;

			try
			{
				string  sConnect;
				sConnect = string.Format("Server={0};Database={1};{2}",
					"localhost", 
					"Sales",
					"Integrated Security=SSPI");  // 搖id=sa;pwd=????				dbConn = new SQL.SqlConnection(sConnect);

				//
				// be sure to escape values when working with text fields!
				//
				string  sql;
				sql = string.Format("Delete from Customers where FirstName='{0}' and LastName='{1}';",
					bc.FirstName.Replace("'", "''"),
					bc.LastName.Replace("'", "''"));

				SQL.SqlCommand  dbCmd;
				dbCmd = new SQL.SqlCommand(sql, dbConn);

				int  rows;
				
				dbConn.Open();
				  rows = dbCmd.ExecuteNonQuery();
				dbConn.Close();

				if (rows != 1)
					throw new System.ApplicationException("Delete failed?!");

				// update global collection and rebind to list box...
				Globals.Customers.Remove(bc);
				this.resetGUI();

				MessageBox.Show("Deleted!");
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally 
			{
				try{ dbConn.Close(); } catch{}
			}
		}

		private void resetGUI()
		{
			this.lstCustomers.DataSource = null;
			this.lstCustomers.DataSource = Globals.Customers;

			foreach (Control c in this.Controls)
				if (c is TextBox)
					((TextBox)c).Clear();
		}

	}//class
}//namespace

⌨️ 快捷键说明

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