📄 currency.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Net;
using System.IO;
using System.Security;
namespace SmartCurrency
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private DataSet ds;
private Hashtable ht;
// DataSet Table, Columns and other strings
private const string CURRENCY = "Currency";
private const string SYMBOL = "Sym";
private const string RATE = "Rate";
private const string folderPersonal = @"\My Documents\Personal";
private const string folderPrivat = @"\My Documents\Privat";
private const string currencyStore = "CurrencyRates.xml";
// MessageBox titles
private const string unavailableWS = "Web Service unavailable";
private const string baseNotSet = "Base currency unknown";
private const string baseDeleted = "Base currency deleted";
private const string attentionPlease = "Attention please";
private const string attentionInitialize = "Initializing list";
private const string applicationError = "Application error";
private const string fileError = "File error";
private const string wrongFormat = "Wrong format";
// MessageBox messages
private const string messageCheckNetwork = "Please check your network connectivity!";
private const string checkCheckBoxWS = "Web Service access is disabled. Do you want to go online and check the corresponding Check-Box?";
private const string messageBaseCurrency = "Please select your preferred base currency";
private const string messageBaseDeleted = "Please select a new base currency and update your preferences";
private const string messageCurrencyExists = "The currency you selected, already exists in your preferences";
private const string messageCreatePreferences = "In order to use this tool, please create your currency prefences";
private const string messageUpdatePreferences = "Update your preferences!";
private const string messageMyPreferences = "My preferences (selected currencies)";
private const string messageCheckYourInput = "Please note, the input should consist of digits";
private const string messageCheckYourInput2 = "The input provided is too large";
private string currencyXmlFile;
private DataView viewFrom;
private DataView viewTo;
private DataView viewBox;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox comboBoxCurr;
private System.Windows.Forms.Button buttonAdd;
private System.Windows.Forms.ListBox listBoxCurr;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button buttonSave;
private System.Windows.Forms.CheckBox checkBoxWS;
private System.Windows.Forms.TextBox textBoxBase;
private System.Windows.Forms.Button buttonBase;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ComboBox comboBoxFrom;
private System.Windows.Forms.ComboBox comboBoxTo;
private System.Windows.Forms.TextBox textBoxValue;
private System.Windows.Forms.TextBox textBoxResult;
private System.Windows.Forms.Button buttonConvert;
private System.Windows.Forms.CheckBox checkBoxOff;
private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Button buttonRemove;
private System.Windows.Forms.PictureBox pictureBoxWarning;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button buttonClear;
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
InitializeComponent();
// Adjust personal/privat folder usage for offline store
// Of course this is a localization issue and should be solved
// in a more general manner (culture)
// *****************************************************
checkPrivateFolders();
// Creating the empty DataSet, the table and their columns
// *******************************************************
ds = new DataSet("MyCurrency");
DataTable dt = new DataTable(CURRENCY);
dt.Columns.Add(SYMBOL, typeof(string));
dt.Columns.Add(RATE, typeof(double));
// Set primary key on the table
// *************************
DataColumn[] key = new DataColumn[1];
key[0] = dt.Columns[SYMBOL];
dt.PrimaryKey = key;
// Add/Integrate the table into the DataSet
// ****************************************
ds.Tables.Add(dt);
}
/// <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.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.checkBoxOff = new System.Windows.Forms.CheckBox();
this.buttonConvert = new System.Windows.Forms.Button();
this.textBoxResult = new System.Windows.Forms.TextBox();
this.textBoxValue = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.comboBoxTo = new System.Windows.Forms.ComboBox();
this.comboBoxFrom = new System.Windows.Forms.ComboBox();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.pictureBoxWarning = new System.Windows.Forms.PictureBox();
this.buttonRemove = new System.Windows.Forms.Button();
this.buttonBase = new System.Windows.Forms.Button();
this.textBoxBase = new System.Windows.Forms.TextBox();
this.checkBoxWS = new System.Windows.Forms.CheckBox();
this.buttonSave = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.listBoxCurr = new System.Windows.Forms.ListBox();
this.buttonAdd = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.comboBoxCurr = new System.Windows.Forms.ComboBox();
this.inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
this.buttonClear = new System.Windows.Forms.Button();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(240, 272);
this.tabControl1.GotFocus += new System.EventHandler(this.tabControl1_GotFocus);
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
//
// tabPage1
//
this.tabPage1.Controls.Add(this.buttonClear);
this.tabPage1.Controls.Add(this.pictureBox1);
this.tabPage1.Controls.Add(this.label7);
this.tabPage1.Controls.Add(this.label6);
this.tabPage1.Controls.Add(this.checkBoxOff);
this.tabPage1.Controls.Add(this.buttonConvert);
this.tabPage1.Controls.Add(this.textBoxResult);
this.tabPage1.Controls.Add(this.textBoxValue);
this.tabPage1.Controls.Add(this.label5);
this.tabPage1.Controls.Add(this.label4);
this.tabPage1.Controls.Add(this.comboBoxTo);
this.tabPage1.Controls.Add(this.comboBoxFrom);
this.tabPage1.Location = new System.Drawing.Point(4, 4);
this.tabPage1.Size = new System.Drawing.Size(232, 246);
this.tabPage1.Text = "Conversion";
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(208, 4);
this.pictureBox1.Size = new System.Drawing.Size(16, 16);
//
// label7
//
this.label7.Location = new System.Drawing.Point(116, 56);
this.label7.Size = new System.Drawing.Size(48, 16);
this.label7.Text = "Result:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(116, 8);
this.label6.Size = new System.Drawing.Size(72, 16);
this.label6.Text = "Input: ";
//
// checkBoxOff
//
this.checkBoxOff.Checked = true;
this.checkBoxOff.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBoxOff.Location = new System.Drawing.Point(12, 104);
this.checkBoxOff.Size = new System.Drawing.Size(96, 16);
this.checkBoxOff.Text = "Stay offline";
//
// buttonConvert
//
this.buttonConvert.Location = new System.Drawing.Point(116, 96);
this.buttonConvert.Size = new System.Drawing.Size(64, 24);
this.buttonConvert.Text = "Convert";
this.buttonConvert.Click += new System.EventHandler(this.buttonConvert_Click);
//
// textBoxResult
//
this.textBoxResult.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
this.textBoxResult.Location = new System.Drawing.Point(116, 72);
this.textBoxResult.ReadOnly = true;
this.textBoxResult.Size = new System.Drawing.Size(96, 20);
this.textBoxResult.Text = "";
//
// textBoxValue
//
this.textBoxValue.Location = new System.Drawing.Point(116, 24);
this.textBoxValue.Size = new System.Drawing.Size(96, 20);
this.textBoxValue.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(12, 56);
this.label5.Size = new System.Drawing.Size(80, 16);
this.label5.Text = "To:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(12, 8);
this.label4.Size = new System.Drawing.Size(100, 16);
this.label4.Text = "From:";
//
// comboBoxTo
//
this.comboBoxTo.Location = new System.Drawing.Point(12, 72);
this.comboBoxTo.Size = new System.Drawing.Size(100, 21);
this.comboBoxTo.SelectedValueChanged += new System.EventHandler(this.comboBoxTo_SelectedValueChanged);
//
// comboBoxFrom
//
this.comboBoxFrom.Location = new System.Drawing.Point(12, 24);
this.comboBoxFrom.Size = new System.Drawing.Size(100, 21);
this.comboBoxFrom.SelectedValueChanged += new System.EventHandler(this.comboBoxTo_SelectedValueChanged);
//
// tabPage2
//
this.tabPage2.Controls.Add(this.pictureBoxWarning);
this.tabPage2.Controls.Add(this.buttonRemove);
this.tabPage2.Controls.Add(this.buttonBase);
this.tabPage2.Controls.Add(this.textBoxBase);
this.tabPage2.Controls.Add(this.checkBoxWS);
this.tabPage2.Controls.Add(this.buttonSave);
this.tabPage2.Controls.Add(this.label2);
this.tabPage2.Controls.Add(this.listBoxCurr);
this.tabPage2.Controls.Add(this.buttonAdd);
this.tabPage2.Controls.Add(this.label1);
this.tabPage2.Controls.Add(this.comboBoxCurr);
this.tabPage2.Location = new System.Drawing.Point(4, 4);
this.tabPage2.Size = new System.Drawing.Size(232, 246);
this.tabPage2.Text = "Options";
//
// pictureBoxWarning
//
this.pictureBoxWarning.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxWarning.Image")));
this.pictureBoxWarning.Location = new System.Drawing.Point(208, 96);
this.pictureBoxWarning.Size = new System.Drawing.Size(16, 16);
this.pictureBoxWarning.Visible = false;
//
// buttonRemove
//
this.buttonRemove.Location = new System.Drawing.Point(108, 140);
this.buttonRemove.Size = new System.Drawing.Size(68, 20);
this.buttonRemove.Text = "Remove";
this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
//
// buttonBase
//
this.buttonBase.Location = new System.Drawing.Point(108, 116);
this.buttonBase.Size = new System.Drawing.Size(68, 20);
this.buttonBase.Text = "Set base";
this.buttonBase.Click += new System.EventHandler(this.buttonBase_Click);
//
// textBoxBase
//
this.textBoxBase.Location = new System.Drawing.Point(180, 116);
this.textBoxBase.Size = new System.Drawing.Size(44, 20);
this.textBoxBase.Text = "";
//
// checkBoxWS
//
this.checkBoxWS.Location = new System.Drawing.Point(112, 68);
this.checkBoxWS.Size = new System.Drawing.Size(112, 20);
this.checkBoxWS.Text = "Use Web Service";
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(108, 92);
this.buttonSave.Size = new System.Drawing.Size(92, 20);
this.buttonSave.Text = "Update list";
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
//
// label2
//
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular);
this.label2.Location = new System.Drawing.Point(8, 48);
this.label2.Size = new System.Drawing.Size(216, 20);
this.label2.Text = "My preferences (selected currencies)";
//
// listBoxCurr
//
this.listBoxCurr.Location = new System.Drawing.Point(8, 68);
this.listBoxCurr.Size = new System.Drawing.Size(96, 93);
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(108, 20);
this.buttonAdd.Size = new System.Drawing.Size(68, 20);
this.buttonAdd.Text = "Add";
this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular);
this.label1.Location = new System.Drawing.Point(8, 4);
this.label1.Size = new System.Drawing.Size(136, 16);
this.label1.Text = "All available currencies";
//
// comboBoxCurr
//
this.comboBoxCurr.Location = new System.Drawing.Point(8, 20);
this.comboBoxCurr.Size = new System.Drawing.Size(96, 21);
this.comboBoxCurr.GotFocus += new System.EventHandler(this.comboBoxCurr_GotFocus);
//
// inputPanel1
//
this.inputPanel1.EnabledChanged += new System.EventHandler(this.inputPanel1_EnabledChanged);
//
// buttonClear
//
this.buttonClear.Location = new System.Drawing.Point(184, 48);
this.buttonClear.Size = new System.Drawing.Size(28, 20);
this.buttonClear.Text = "C";
this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click);
//
// Form1
//
this.Controls.Add(this.tabControl1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu1;
this.Text = "Currency ";
this.GotFocus += new System.EventHandler(this.Form1_GotFocus);
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void buttonAdd_Click(object sender, System.EventArgs e)
{
WebServiceX.CurrencyConvertor ws = new WebServiceX.CurrencyConvertor();
DataRow row = ds.Tables[CURRENCY].NewRow();
row[SYMBOL] = comboBoxCurr.SelectedItem.ToString();
if (ds.Tables[CURRENCY].Rows.Contains(row[SYMBOL]) == false)
{
if (checkBoxWS.Checked == true && textBoxBase.Text.Length != 0)
{
if (comboBoxCurr.SelectedItem.ToString() == textBoxBase.Text)
row[RATE] = 1;
else
row[RATE] = ws.ConversionRate(getCurrency(textBoxBase.Text), getCurrency(comboBoxCurr.SelectedItem.ToString()));
}
else
{
raiseAttention(true);
row[RATE] = 0;
}
try
{
ds.Tables[CURRENCY].Rows.Add(row);
if (ds.Tables[CURRENCY].Rows.Count == 1)
{
// the first select currency will be the base currency
textBoxBase.Text = listBoxCurr.Text;
}
}
catch (System.Data.ConstraintException)
{
MessageBox.Show(messageCurrencyExists, attentionPlease, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
}
}
else
MessageBox.Show(messageCurrencyExists, attentionPlease, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
inputPanel1.Enabled = false;
}
private void Form1_Load(object sender, System.EventArgs e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -