📄 form1.cs
字号:
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;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem_EditAccount;
private System.Windows.Forms.MenuItem menuItem_EditType;
private System.Windows.Forms.MenuItem menuItem_Archive;
private System.Windows.Forms.MenuItem menuItem_DeleteAccount;
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.menuItem1 = 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.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem_EditAccount = new System.Windows.Forms.MenuItem();
this.menuItem_EditType = new System.Windows.Forms.MenuItem();
this.menuItem_Archive = new System.Windows.Forms.MenuItem();
this.menuItem_DeleteAccount = 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.menuItem1);
this.menuItem2.MenuItems.Add(this.menuItem3);
this.menuItem2.MenuItems.Add(this.menuItem_Archive);
this.menuItem2.MenuItems.Add(this.menuItem_DeleteAccount);
this.menuItem2.Text = "Action";
//
// menuItem1
//
this.menuItem1.MenuItems.Add(this.menuItem_NewTransaction);
this.menuItem1.MenuItems.Add(this.menuItem_Transfer);
this.menuItem1.MenuItems.Add(this.menuItem_NewAccount);
this.menuItem1.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);
//
// menuItem3
//
this.menuItem3.MenuItems.Add(this.menuItem_EditAccount);
this.menuItem3.MenuItems.Add(this.menuItem_EditType);
this.menuItem3.Text = "Edit";
//
// menuItem_EditAccount
//
this.menuItem_EditAccount.Text = "Account";
this.menuItem_EditAccount.Click += new System.EventHandler(this.menuItem_EditAccount_Click);
//
// menuItem_EditType
//
this.menuItem_EditType.Text = "Expense Types";
this.menuItem_EditType.Click += new System.EventHandler(this.menuItem_EditType_Click);
//
// menuItem_Archive
//
this.menuItem_Archive.Text = "Archive All";
this.menuItem_Archive.Click += new System.EventHandler(this.menuItem_Archive_Click);
//
// menuItem_DeleteAccount
//
this.menuItem_DeleteAccount.Text = "Delete Account";
this.menuItem_DeleteAccount.Click += new System.EventHandler(this.menuItem_DeleteAccount_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();
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 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;
}
private void menuItem_EditAccount_Click(object sender, System.EventArgs e)
{
if(this.listView1.SelectedIndices.Count>0)
{
int index=this.listView1.SelectedIndices[0];
string acntname=this.listView1.Items[index].SubItems[0].Text;
MvmMoney.EditAccount form=new EditAccount(acntname);
form.ShowDialog();
}
else
{
MessageBox.Show("No account selected.","Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
}
}
private void menuItem_EditType_Click(object sender, System.EventArgs e)
{
MvmMoney.TypeEditForm form=new TypeEditForm();
form.ShowDialog();
}
private void menuItem_DeleteAccount_Click(object sender, System.EventArgs e)
{
if(this.listView1.SelectedIndices.Count>0)
{
int index=this.listView1.SelectedIndices[0];
string accountName=this.listView1.Items[index].SubItems[0].Text;
System.Windows.Forms.DialogResult result=System.Windows.Forms.MessageBox.Show("The account \""+accountName+"\" and all associated transactions will be permanently deleted. Are you sure to continue?","Deleting",System.Windows.Forms.MessageBoxButtons.OKCancel,System.Windows.Forms.MessageBoxIcon.Question,System.Windows.Forms.MessageBoxDefaultButton.Button1);
if(result==System.Windows.Forms.DialogResult.OK)
{
Form1.db.DeleteAccount(accountName);
}
}
}
private void menuItem_Archive_Click(object sender, System.EventArgs e)
{
try
{
if(this.listView1.Items.Count==0)
{
System.Windows.Forms.MessageBox.Show("Cannot clear and archive. The account list is empty.","Information",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Asterisk,System.Windows.Forms.MessageBoxDefaultButton.Button1);
return;
}
string postfix=System.DateTime.Now.Month+"-"+System.DateTime.Now.Day+"-"+System.DateTime.Now.Hour+"-"+System.DateTime.Now.Minute+"-"+System.DateTime.Now.Second;
string archiveFile="Archive-"+postfix+".csv";
string message="All the transactions will be cleared and archived to the file "+archiveFile+". You can copy the archive file(s) via ActiveSync and make further report in Excel.\r\n\r\nDo you want to continue?";
DialogResult result=MessageBox.Show(message,"Clear & Archive",MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
if(result==DialogResult.OK)
{
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
System.IO.FileStream stream=System.IO.File.Open(path+"\\"+archiveFile,System.IO.FileMode.OpenOrCreate);
System.IO.StreamWriter writer=new System.IO.StreamWriter(stream);
for(int i=0;i<this.listView1.Items.Count;i++)
{
string accountname=this.listView1.Items[i].SubItems[0].Text;
int accountId=Form1.db.GetAccountId(accountname);
DataRow[] rows=Form1.db.GetTransactions(accountId);
foreach(DataRow row in rows)
{
writer.WriteLine(row["ID"]+","+row["Type"]+","+row["Amount"]+","+System.DateTime.Parse(row["Date"].ToString()).ToShortDateString()+","+row["Note"]);
}
Form1.db.ClearTransactions(accountId);
}
writer.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message+"\r\n"+ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
}
}
}
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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -