📄 transactionlistform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MvmMoney
{
/// <summary>
/// Summary description for TransactionListForm.
/// </summary>
public class TransactionListForm : System.Windows.Forms.Form, IMoneyDbChangeListener
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.MenuItem menuItem_Done;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem_EditAccount;
private System.Windows.Forms.MenuItem menuItem_DeleteAccount;
private System.Windows.Forms.MenuItem menuItem_Transfer;
private System.Windows.Forms.MenuItem menuItem_Archive;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem_NewTransaction;
private System.Windows.Forms.MenuItem menuItem_DeleteTransaction;
private System.Windows.Forms.MainMenu mainMenu1;
public TransactionListForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.listView1.Font=new Font("Nina",10,System.Drawing.FontStyle.Regular);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
Form1.db.RemoveChangeListener(this);
}
#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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem_Done = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem_EditAccount = new System.Windows.Forms.MenuItem();
this.menuItem_DeleteAccount = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem_NewTransaction = new System.Windows.Forms.MenuItem();
this.menuItem_DeleteTransaction = new System.Windows.Forms.MenuItem();
this.menuItem_Transfer = new System.Windows.Forms.MenuItem();
this.menuItem_Archive = new System.Windows.Forms.MenuItem();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem_Done);
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// menuItem_Done
//
this.menuItem_Done.Text = "Done";
this.menuItem_Done.Click += new System.EventHandler(this.menuItem_Done_Click);
//
// menuItem1
//
this.menuItem1.MenuItems.Add(this.menuItem2);
this.menuItem1.MenuItems.Add(this.menuItem3);
this.menuItem1.MenuItems.Add(this.menuItem_Transfer);
this.menuItem1.MenuItems.Add(this.menuItem_Archive);
this.menuItem1.Text = "Action";
//
// menuItem2
//
this.menuItem2.MenuItems.Add(this.menuItem_EditAccount);
this.menuItem2.MenuItems.Add(this.menuItem_DeleteAccount);
this.menuItem2.Text = "Current Account";
//
// menuItem_EditAccount
//
this.menuItem_EditAccount.Text = "Rename";
this.menuItem_EditAccount.Click += new System.EventHandler(this.menuItem_EditAccount_Click);
//
// menuItem_DeleteAccount
//
this.menuItem_DeleteAccount.Text = "Delete";
this.menuItem_DeleteAccount.Click += new System.EventHandler(this.menuItem_DeleteAccount_Click);
//
// menuItem3
//
this.menuItem3.MenuItems.Add(this.menuItem_NewTransaction);
this.menuItem3.MenuItems.Add(this.menuItem_DeleteTransaction);
this.menuItem3.Text = "Transaction";
//
// menuItem_NewTransaction
//
this.menuItem_NewTransaction.Text = "New";
this.menuItem_NewTransaction.Click += new System.EventHandler(this.menuItem_NewTransaction_Click);
//
// menuItem_DeleteTransaction
//
this.menuItem_DeleteTransaction.Text = "Delete";
this.menuItem_DeleteTransaction.Click += new System.EventHandler(this.menuItem_DeleteTransaction_Click);
//
// menuItem_Transfer
//
this.menuItem_Transfer.Text = "Transfer";
this.menuItem_Transfer.Click += new System.EventHandler(this.menuItem_Transfer_Click);
//
// menuItem_Archive
//
this.menuItem_Archive.Text = "Clear and Archive";
this.menuItem_Archive.Click += new System.EventHandler(this.menuItem_Archive_Click);
//
// listView1
//
this.listView1.Columns.Add(this.columnHeader3);
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);
//
// columnHeader3
//
this.columnHeader3.Text = "ID";
this.columnHeader3.Width = 30;
//
// columnHeader1
//
this.columnHeader1.Text = "Date";
this.columnHeader1.Width = 75;
//
// columnHeader2
//
this.columnHeader2.Text = "Amount";
this.columnHeader2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.columnHeader2.Width = 63;
//
// TransactionListForm
//
this.Controls.Add(this.listView1);
this.Menu = this.mainMenu1;
this.Load += new System.EventHandler(this.TransactionListForm_Load);
}
#endregion
private void TransactionListForm_Load(object sender, System.EventArgs e)
{
Form1.db.RegisterChangeListener(this);
}
private void menuItem_Done_Click(object sender, System.EventArgs e)
{
this.Close();
}
public void SetAccount(string AccountName)
{
this.Text=AccountName;
this.ShowTransactionData();
}
private void ShowTransactionData()
{
this.listView1.Items.Clear();
DataRow[] rows=Form1.db.GetTransactions(Form1.db.GetAccountId(this.Text));
foreach(DataRow row in rows)
{
System.Windows.Forms.ListViewItem item=new ListViewItem(new string[]{row["ID"].ToString(),System.DateTime.Parse(row["Date"].ToString()).ToShortDateString(),row["Amount"].ToString()});
this.listView1.Items.Insert(0,item);
}
}
public void OnAccountChanged()
{
//do nothing.
}
public void OnBalanceChanged()
{
//do nothing.
}
public void OnTransactionChanged()
{
this.ShowTransactionData();
}
private void menuItem_Archive_Click(object sender, System.EventArgs e)
{
if(this.listView1.Items.Count==0)
{
System.Windows.Forms.MessageBox.Show("Cannot clear and archive. The transaction list is empty.","Information",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Asterisk,System.Windows.Forms.MessageBoxDefaultButton.Button1);
return;
}
int accountId=Form1.db.GetAccountId(this.Text);
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-"+Form1.db.GetAccountId(this.Text)+"-"+postfix+".csv";
string message="The transactions of the account \""+this.Text+"\" 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);
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"]);
}
writer.Close();
Form1.db.ClearTransactions(accountId);
}
}
private void menuItem_Transfer_Click(object sender, System.EventArgs e)
{
MvmMoney.TransferForm form=new TransferForm();
form.SetDefaultAccount(this.Text);
form.ShowDialog();
}
private void menuItem_NewTransaction_Click(object sender, System.EventArgs e)
{
MvmMoney.NewTransactionForm form=new NewTransactionForm();
form.SetDefaultAccount(this.Text);
form.ShowDialog();
}
private void listView1_ItemActivate(object sender, System.EventArgs e)
{
if(this.listView1.SelectedIndices.Count>0)
{
int index=this.listView1.SelectedIndices[0];
int transID=System.Int32.Parse(this.listView1.Items[index].SubItems[0].Text);
DataRow row=Form1.db.GetTransactionInfo(transID);
string message="Transaction Infomation:\r\n\r\n";
message+="Account: "+this.Text+"\r\n";
message+="Date: "+System.DateTime.Parse(row["Date"].ToString()).ToShortDateString()+"\r\n";
message+="Type: "+row["Type"]+"\r\n";
message+="Amount: "+row["Amount"]+"\r\n";
message+="Note: "+row["Note"];
System.Windows.Forms.MessageBox.Show(message,"Transaction",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.None,System.Windows.Forms.MessageBoxDefaultButton.Button1);
}
}
private void menuItem_DeleteTransaction_Click(object sender, System.EventArgs e)
{
if(this.listView1.SelectedIndices.Count>0)
{
int index=this.listView1.SelectedIndices[0];
string message="Permanently delete this transaction? \r\n\r\nDate: "+this.listView1.Items[index].SubItems[1].Text+"\r\nAmount: "+this.listView1.Items[index].SubItems[2].Text;
System.Windows.Forms.DialogResult result=System.Windows.Forms.MessageBox.Show(message,"Deleting Transaction",System.Windows.Forms.MessageBoxButtons.OKCancel,System.Windows.Forms.MessageBoxIcon.Question,System.Windows.Forms.MessageBoxDefaultButton.Button1);
if(result==System.Windows.Forms.DialogResult.OK)
{
Form1.db.DeleteTransaction(System.Int32.Parse(this.listView1.Items[index].SubItems[0].Text));
}
}
}
private void menuItem_DeleteAccount_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.DialogResult result=System.Windows.Forms.MessageBox.Show("The account \""+this.Text+"\" 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(this.Text);
}
this.Close();
}
private void menuItem_EditAccount_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Renaming account is not supported in this version.","Not supported",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Exclamation,System.Windows.Forms.MessageBoxDefaultButton.Button1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -