📄 newtransactionform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace MvmMoney
{
/// <summary>
/// Summary description for NewTransactionForm.
/// </summary>
public class NewTransactionForm : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem_Done;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox comboBox_Accounts;
private System.Windows.Forms.ComboBox comboBox_Types;
private System.Windows.Forms.TextBox textBox_Amount;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox_Note;
private System.Windows.Forms.ComboBox comboBox_EI;
private System.Windows.Forms.MenuItem menuItem_Cancel;
public NewTransactionForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
DataTable accounts=Form1.db.GetAccounts();
foreach(DataRow row in accounts.Rows)
{
this.comboBox_Accounts.Items.Add(row["Name"]);
}
foreach(string typename in Form1.db.GetTypes())
{
this.comboBox_Types.Items.Add(typename);
}
this.SetInputMode(this.textBox_Amount,InputMode.Numbers);
this.comboBox_Accounts.Focus();
this.comboBox_EI.SelectedIndex=0;
this.comboBox_Accounts.SelectedIndex=0;
this.comboBox_Types.SelectedIndex=0;
}
/// <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()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem_Done = new System.Windows.Forms.MenuItem();
this.menuItem_Cancel = new System.Windows.Forms.MenuItem();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.comboBox_Accounts = new System.Windows.Forms.ComboBox();
this.comboBox_Types = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox_Amount = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.comboBox_EI = new System.Windows.Forms.ComboBox();
this.label5 = new System.Windows.Forms.Label();
this.textBox_Note = new System.Windows.Forms.TextBox();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem_Done);
this.mainMenu1.MenuItems.Add(this.menuItem_Cancel);
//
// menuItem_Done
//
this.menuItem_Done.Text = "Done";
this.menuItem_Done.Click += new System.EventHandler(this.menuItem_Done_Click);
//
// menuItem_Cancel
//
this.menuItem_Cancel.Text = "Cancel";
this.menuItem_Cancel.Click += new System.EventHandler(this.menuItem_Cancel_Click);
//
// label1
//
this.label1.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Bold);
this.label1.Location = new System.Drawing.Point(6, 6);
this.label1.Size = new System.Drawing.Size(152, 18);
this.label1.Text = "Account";
//
// label2
//
this.label2.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Bold);
this.label2.Location = new System.Drawing.Point(6, 48);
this.label2.Size = new System.Drawing.Size(152, 18);
this.label2.Text = "Type";
//
// comboBox_Accounts
//
this.comboBox_Accounts.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
this.comboBox_Accounts.Location = new System.Drawing.Point(6, 24);
this.comboBox_Accounts.Size = new System.Drawing.Size(162, 24);
//
// comboBox_Types
//
this.comboBox_Types.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
this.comboBox_Types.Location = new System.Drawing.Point(6, 66);
this.comboBox_Types.Size = new System.Drawing.Size(162, 24);
//
// label3
//
this.label3.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Bold);
this.label3.Location = new System.Drawing.Point(6, 90);
this.label3.Size = new System.Drawing.Size(60, 18);
this.label3.Text = "Amount";
//
// textBox_Amount
//
this.textBox_Amount.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
this.textBox_Amount.Location = new System.Drawing.Point(6, 108);
this.textBox_Amount.Size = new System.Drawing.Size(54, 24);
this.textBox_Amount.Text = "0.0";
this.textBox_Amount.LostFocus += new System.EventHandler(this.textBox_Amount_LostFocus);
//
// label4
//
this.label4.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Bold);
this.label4.Location = new System.Drawing.Point(66, 90);
this.label4.Size = new System.Drawing.Size(108, 18);
this.label4.Text = "Expense/Income";
//
// comboBox_EI
//
this.comboBox_EI.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
this.comboBox_EI.Items.Add("Expense");
this.comboBox_EI.Items.Add("Income");
this.comboBox_EI.Location = new System.Drawing.Point(66, 108);
this.comboBox_EI.Size = new System.Drawing.Size(102, 24);
//
// label5
//
this.label5.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Bold);
this.label5.Location = new System.Drawing.Point(6, 132);
this.label5.Size = new System.Drawing.Size(152, 18);
this.label5.Text = "Note";
//
// textBox_Note
//
this.textBox_Note.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);
this.textBox_Note.Location = new System.Drawing.Point(6, 150);
this.textBox_Note.Multiline = true;
this.textBox_Note.Size = new System.Drawing.Size(162, 24);
this.textBox_Note.Text = "";
//
// NewTransactionForm
//
this.Controls.Add(this.comboBox_Accounts);
this.Controls.Add(this.comboBox_Types);
this.Controls.Add(this.textBox_Amount);
this.Controls.Add(this.comboBox_EI);
this.Controls.Add(this.textBox_Note);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.label5);
this.Menu = this.mainMenu1;
this.Text = "New Transaction";
this.Load += new System.EventHandler(this.NewTransactionForm_Load);
}
#endregion
private void menuItem_Cancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem_Done_Click(object sender, System.EventArgs e)
{
double amount;
try
{
amount=System.Double.Parse(this.textBox_Amount.Text);
amount=(System.Math.Ceiling(amount*100))/100;
this.textBox_Amount.Text=amount+"";
if(amount==0)
{
MessageBox.Show("Amount should not be zero.","Error",MessageBoxButtons.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
return;
}
}
catch(Exception)
{
MessageBox.Show("Invalid amount.","Error",MessageBoxButtons.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
return;
}
if(this.comboBox_EI.SelectedIndex==0)
{
amount=System.Math.Abs(amount)*-1;
}
else
{
amount=System.Math.Abs(amount);
}
Form1.db.NewTransaction(this.comboBox_Types.SelectedItem.ToString(),Form1.db.GetAccountId(this.comboBox_Accounts.SelectedItem.ToString()),amount,System.DateTime.Now,this.textBox_Note.Text);
this.Close();
}
// Interop declarations
[DllImport("coredll.dll", EntryPoint="GetCapture")]
private static extern IntPtr GetCapture();
[DllImport("coredll.dll", EntryPoint="GetWindow")]
private static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("coredll.dll", EntryPoint="SendMessage")]
private static extern uint SendMessage(IntPtr hWnd, uint msg, uint wParam,
uint lParam);
// Constants required for interop
const int GW_CHILD = 5;
const uint EM_SETINPUTMODE = 0x00DE;
public void SetInputMode(Control ctrl, InputMode mode)
{
// Get the handle for the current control
ctrl.Capture = true;
IntPtr h = GetCapture();
ctrl.Capture = false;
// Get the child window for the control
IntPtr hEditbox = GetWindow(h, GW_CHILD);
// Set the input mode
SendMessage(hEditbox, EM_SETINPUTMODE, 0, (uint)mode);
}
// Input mode enumeration
public enum InputMode
{
Spell = 0,
T9 = 1,
Numbers = 2,
Text = 3
}
private void NewTransactionForm_Load(object sender, System.EventArgs e)
{
}
public void SetDefaultAccount(string accountName)
{
this.comboBox_Accounts.SelectedIndex=this.comboBox_Accounts.Items.IndexOf(accountName);
}
private void textBox_Amount_LostFocus(object sender, System.EventArgs e)
{
double amount;
try
{
amount=System.Double.Parse(this.textBox_Amount.Text);
amount=(System.Math.Ceiling(amount*100))/100;
this.textBox_Amount.Text=amount+"";
}
catch(Exception)
{
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -