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

📄 form1.cs

📁 国人开发的电子钱包
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace MvmMoney
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form,IMoneyDbChangeListener
	{
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItemExit;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.MenuItem menuItem_NewAccount;
		private System.Windows.Forms.MenuItem menuItem_Transfer;
		static public MoneyDb db;
		private System.Windows.Forms.MenuItem menuItem_NewTransaction;
		private System.Windows.Forms.Timer timer1;
		private string path;
		public static MvmMoney.Splash splash;
		

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
			InitializeComponent();

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

			this.listView1.Font=new Font("Nina",10,System.Drawing.FontStyle.Regular);
			db=MoneyDb.LoadFromFile(path+"\\moneydb.xml");
			db.RegisterChangeListener(this);
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItemExit = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem_NewTransaction = new System.Windows.Forms.MenuItem();
			this.menuItem_Transfer = new System.Windows.Forms.MenuItem();
			this.menuItem_NewAccount = new System.Windows.Forms.MenuItem();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.timer1 = new System.Windows.Forms.Timer();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.menuItemExit);
			this.mainMenu1.MenuItems.Add(this.menuItem2);
			// 
			// menuItemExit
			// 
			this.menuItemExit.Text = "Exit";
			this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.MenuItems.Add(this.menuItem_NewTransaction);
			this.menuItem2.MenuItems.Add(this.menuItem_Transfer);
			this.menuItem2.MenuItems.Add(this.menuItem_NewAccount);
			this.menuItem2.Text = "New";
			// 
			// menuItem_NewTransaction
			// 
			this.menuItem_NewTransaction.Text = "Transaction";
			this.menuItem_NewTransaction.Click += new System.EventHandler(this.menuItem_NewTransaction_Click);
			// 
			// menuItem_Transfer
			// 
			this.menuItem_Transfer.Text = "Transfer";
			this.menuItem_Transfer.Click += new System.EventHandler(this.menuItem_Transfer_Click);
			// 
			// menuItem_NewAccount
			// 
			this.menuItem_NewAccount.Text = "Account";
			this.menuItem_NewAccount.Click += new System.EventHandler(this.menuItem_NewAccount_Click);
			// 
			// listView1
			// 
			this.listView1.Columns.Add(this.columnHeader1);
			this.listView1.Columns.Add(this.columnHeader2);
			this.listView1.FullRowSelect = true;
			this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.ItemActivate += new System.EventHandler(this.listView1_ItemActivate);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Account";
			this.columnHeader1.Width = 106;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Balance";
			this.columnHeader2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.columnHeader2.Width = 68;
			// 
			// timer1
			// 
			this.timer1.Interval = 300000;
			// 
			// Form1
			// 
			this.Controls.Add(this.listView1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu1;
			this.Text = "Mvm Money";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Form1.splash=new Splash();
			Form1.splash.Show();
			Form1.splash.Refresh();
			Application.Run(new Form1());
		}

		private void menuItemExit_Click(object sender, System.EventArgs e)
		{
			Form1.db.CommitToFile();
			this.Close();
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.ShowAccounts();
			this.timer1.Interval=180000; //three minutes
			this.timer1.Enabled=true;
			this.timer1.Tick+=new EventHandler(timer1_Tick);
			Form1.splash.Hide();
			Form1.splash.Dispose();
		}

		public void ShowAccounts()
		{
			this.listView1.Items.Clear();
			System.Data.DataTable dt=db.GetAccounts();
			foreach(DataRow row in dt.Rows)
			{
				ListViewItem item=new ListViewItem(new string[]{row["Name"].ToString(),row["Balance"].ToString()});
				this.listView1.Items.Add(item);
			}
		}

		private void menuItem_NewAccount_Click(object sender, System.EventArgs e)
		{
			MvmMoney.NewAccountForm form=new NewAccountForm();
			form.ShowDialog();
		}

		private void menuItem_NewTransaction_Click(object sender, System.EventArgs e)
		{
			if(this.listView1.Items.Count>=1)
			{
				MvmMoney.NewTransactionForm form=new NewTransactionForm();
				if(this.listView1.SelectedIndices.Count>0)
				{
					int index=this.listView1.SelectedIndices[0];
					form.SetDefaultAccount(this.listView1.Items[index].SubItems[0].Text);
				}
				form.ShowDialog();
			}
			else
			{
				MessageBox.Show("There is no account. Please create accounts first.","No Account",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
			}
		}

		private void menuItem_Transfer_Click(object sender, System.EventArgs e)
		{
			if(this.listView1.Items.Count>=2)
			{
				MvmMoney.TransferForm form=new TransferForm();
				form.ShowDialog();
			}
			else
			{
				MessageBox.Show("There is only one account. Please create another account first.","Only One Account",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
			}
		}

		private void listView1_ItemActivate(object sender, System.EventArgs e)
		{
			if(this.listView1.SelectedIndices.Count>0)
			{
				string AccountName=this.listView1.Items[this.listView1.SelectedIndices[0]].SubItems[0].Text;
				this.OpenAccount(AccountName);
			}
			
		}

		private void OpenAccount(string AccountName)
		{
			//System.Windows.Forms.MessageBox.Show("account "+AccountName+", id="+this.db.GetAccountId(AccountName));
			MvmMoney.TransactionListForm form=new TransactionListForm();
			form.SetAccount(AccountName);
			form.ShowDialog();
		}

		public void OnAccountChanged()
		{
			this.ShowAccounts();
		}

		public void OnBalanceChanged()
		{
			this.ShowAccounts();
		}
		
		public void OnTransactionChanged()
		{
			//do nothing.
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			Form1.db.CommitToFile();
			this.timer1.Enabled=true;
		}
	}

	public interface IMoneyDbChangeListener
	{
		void OnBalanceChanged();
		void OnAccountChanged();
		void OnTransactionChanged();
	}

	public class MoneyDb
	{
		private DataSet ds;
		private System.Collections.ArrayList ChangeListners=new ArrayList();
		private string sourcefile;

		private bool ChangedFlag=false;
		public void SetChangedFlag()
		{
			lock(this.ChangeListners)
			{
				this.ChangedFlag=true;
			}			
		}
		public void ResetChangedFlag()
		{
			lock(this.ChangeListners)
			{
				this.ChangedFlag=false;
			}
		}
		public bool GetChangedFlag()
		{
			return this.ChangedFlag;
		}

		public MoneyDb()
		{
			ds=new DataSet();
			DataTable dt_Accounts=new DataTable("Accounts");
			DataTable dt_Transaction=new DataTable("Transaction");
			DataTable dt_TransactionType=new DataTable("TransactionType");

			dt_Accounts.Columns.Add(new DataColumn("AccountID",System.Type.GetType("System.Int32")));
			dt_Accounts.Columns.Add(new DataColumn("Name",System.Type.GetType("System.String")));
			dt_Accounts.Columns.Add(new DataColumn("Balance",System.Type.GetType("System.Double")));
			dt_Accounts.Constraints.Add("PrimaryKey",dt_Accounts.Columns["AccountID"],true);

			dt_TransactionType.Columns.Add(new DataColumn("TypeName",System.Type.GetType("System.String")));
			dt_TransactionType.Constraints.Add("PrimaryKey",dt_TransactionType.Columns["TypeName"],true);

			dt_Transaction.Columns.Add(new DataColumn("ID",System.Type.GetType("System.Int32")));
			dt_Transaction.Columns.Add(new DataColumn("Type",System.Type.GetType("System.String")));
			dt_Transaction.Columns.Add(new DataColumn("Account",System.Type.GetType("System.Int32")));
			dt_Transaction.Columns.Add(new DataColumn("Amount",System.Type.GetType("System.Double")));
			dt_Transaction.Columns.Add(new DataColumn("Date",System.Type.GetType("System.DateTime")));
			dt_Transaction.Columns.Add(new DataColumn("Note",System.Type.GetType("System.String")));
			dt_Transaction.Constraints.Add("TransactionTypeConstraint",dt_TransactionType.Columns["TypeName"],dt_Transaction.Columns["Type"]);
			dt_Transaction.Constraints.Add("AccountIDConstraint",dt_Accounts.Columns["AccountID"],dt_Transaction.Columns["Account"]);

			ds.Tables.AddRange(new DataTable[]{dt_Accounts,dt_TransactionType,dt_Transaction});
			
			this.sourcefile=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )+"\\moneydb.xml";
		}

		public static MoneyDb LoadFromFile(string filename)
		{
			MoneyDb moneydb=new MoneyDb();
			moneydb.ds.ReadXml(filename);
			moneydb.sourcefile=filename;
			return moneydb;
		}

⌨️ 快捷键说明

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