📄 currency.cs
字号:
{
// Initialize list of all available currencies
// *******************************************
initializeAvailableCurrencies();
// Create needed Data-Views based on DataSet
// ******************************************
initializeViews();
// Read the offline rates store (xml) - if exists
// **********************************************
if (File.Exists(currencyXmlFile) == true)
{
try
{
ds.ReadXml(currencyXmlFile);
}
catch (SecurityException exception)
{
MessageBox.Show(exception.Message, applicationError, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
this.Close();
}
if (ds.Tables[CURRENCY].Rows.Count > 0)
{
// Offline store is active and now looking for the base currency
// *******************************************************************
int indx = 0;
foreach (DataRow row in ds.Tables[CURRENCY].Rows)
{
if (Convert.ToInt32(row[RATE]) == 1) // is there any candidate for the base currency?
{
textBoxBase.Text = row[SYMBOL].ToString();
comboBoxFrom.SelectedItem = comboBoxFrom.Items[indx];
}
else if (Convert.ToInt32(row[RATE]) == 0) // Is there any irregular item?
{
// Uninitialized currency rate. Raise attention!
// *********************************************
raiseAttention(true);
}
indx++;
}
comboBoxTo.SelectedItem = comboBoxFrom.Items[0];
tabControl1.SelectedIndex = 0; // ready to use and switch to the main application page
}
else
activateOptionsPage();
}
else
activateOptionsPage();
}
#region PRIVATE_METHODS
/// <summary>
/// Just changes the tab-page index along with an informational message
/// </summary>
void activateOptionsPage()
{
tabControl1.SelectedIndex = 1;
MessageBox.Show(messageCreatePreferences, attentionInitialize, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
}
/// <summary>
/// Show a yellow icon and a label-message advising the user to update the list
/// of preferences as new items arrived.
/// </summary>
/// <param name="activate">true to raise the attention, false for returning to the normal state</param>
void raiseAttention(bool activate)
{
label2.Text = (activate) ? messageUpdatePreferences : messageMyPreferences;
label2.ForeColor = (activate) ? Color.DarkCyan : SystemColors.ControlText;
this.pictureBoxWarning.Visible = activate;
}
/// <summary>
/// Creating the list of all available currencies, sorting the list and creating indexes
/// (using HashTabel) to find the items if requested. After finishing the basic tasks, the
/// hosting ComboBox is filled with the available items.
/// </summary>
void initializeAvailableCurrencies()
{
ArrayList al = new ArrayList(164);
ht = new Hashtable(164);
Cursor.Current = Cursors.WaitCursor;
comboBoxCurr.Items.Clear();
Type currency = typeof(WebServiceX.Currency);
int key = 0;
foreach(FieldInfo s in currency.GetFields())
{
string str = s.Name;
if (str.Length == 3)
{
al.Add(str);
ht.Add(str, key);
key++;
}
}
al.Sort();
for (int i = 0; i < al.Count; i++)
comboBoxCurr.Items.Add(al[i]);
comboBoxCurr.SelectedItem = comboBoxCurr.Items[0];
comboBoxCurr.Refresh();
Cursor.Current = Cursors.Default;
}
/// <summary>
/// On German Pocket PC istead of a "Personal" there is "Privat" folder available.
/// YES - this is just a workaround and not a finished solution.
/// </summary>
private void checkPrivateFolders()
{
try
{
// Try EN and DE folders
if (Directory.Exists(folderPersonal))
currencyXmlFile = folderPersonal + "\\" + currencyStore;
else if (Directory.Exists(folderPrivat))
currencyXmlFile = folderPrivat + "\\" + currencyStore;
else
{ // Create a new one
DirectoryInfo di = Directory.CreateDirectory(folderPersonal);
currencyXmlFile = folderPersonal + "\\" + currencyStore;
}
}
catch (IOException exception)
{
MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
this.Close();
}
catch (UnauthorizedAccessException exception)
{
MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
this.Close();
}
}
/// <summary>
/// Creating data views based on the DataSet instance
/// </summary>
private void initializeViews()
{
viewFrom = new DataView(ds.Tables[CURRENCY]);
viewTo = new DataView(ds.Tables[CURRENCY]);
viewBox = new DataView(ds.Tables[CURRENCY]);
comboBoxFrom.Items.Clear();
comboBoxFrom.DataSource = viewFrom;
comboBoxFrom.DisplayMember = SYMBOL;
comboBoxFrom.ValueMember = RATE;
comboBoxTo.Items.Clear();
comboBoxTo.DataSource = viewTo;
comboBoxTo.DisplayMember = SYMBOL;
comboBoxTo.ValueMember = RATE;
listBoxCurr.Items.Clear();
listBoxCurr.DataSource = viewBox;
listBoxCurr.DisplayMember = SYMBOL;
listBoxCurr.ValueMember = RATE;
}
/// <summary>
/// Converts the currencies from the available string representation to the standard integer based enumeration.
/// </summary>
/// <param name="symbol">The symbol as three character string</param>
/// <returns>The rquested enumeration</returns>
private WebServiceX.Currency getCurrency (string symbol)
{
int i = 0;
if (ht.Contains(symbol))
i = (int)ht[symbol];
return (WebServiceX.Currency)i;
}
#endregion PRIVATE_METHODS
private void buttonSave_Click(object sender, System.EventArgs e)
{
bool accessWebService = false;
if (textBoxBase.Text.Length == 0)
{
MessageBox.Show(messageBaseCurrency, baseNotSet, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
return;
}
if (checkBoxWS.Checked == false)
{
DialogResult dialogResult = MessageBox.Show(checkCheckBoxWS, unavailableWS, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (dialogResult == DialogResult.Cancel)
return;
else if (dialogResult == DialogResult.Yes)
{
checkBoxWS.Checked = true;
accessWebService = true;
}
}
else
accessWebService = true;
Cursor.Current = Cursors.WaitCursor;
try
{
WebServiceX.CurrencyConvertor ws = new WebServiceX.CurrencyConvertor();
foreach (DataRow row1 in ds.Tables[CURRENCY].Rows)
{
if (row1[SYMBOL].ToString() == textBoxBase.Text)
row1[RATE] = 1;
else
{
if (accessWebService == true)
row1[RATE] = ws.ConversionRate(getCurrency(textBoxBase.Text), getCurrency(row1[SYMBOL].ToString()));
// otherwise is rate just left or for the new one is set to 0
}
}
ds.WriteXml(currencyXmlFile);
raiseAttention(!accessWebService);
}
catch (WebException)
{
MessageBox.Show(messageCheckNetwork, unavailableWS, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
catch (IOException exception)
{
MessageBox.Show(exception.Message, fileError, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
catch (SecurityException exception)
{
MessageBox.Show(exception.Message, applicationError, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
this.Close();
}
Cursor.Current = Cursors.Default;
}
private void buttonBase_Click(object sender, System.EventArgs e)
{
if (textBoxBase.Text.Length > 0 && listBoxCurr.Text.Length > 0)
{
// Old base exists, New exists -> thus list should be updated
raiseAttention(true);
}
textBoxBase.Text = listBoxCurr.Text;
}
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
switch (tabControl1.SelectedIndex)
{
case 0:
inputPanel1.Enabled = true;
textBoxValue.Focus();
break;
case 1:
inputPanel1.Enabled = false;
break;
default: // Wonder - should never come here!
break;
}
}
private void buttonConvert_Click(object sender, System.EventArgs e)
{
if (textBoxValue.Text.Length > 0)
{
double from = 0;
try
{
from = Convert.ToDouble(textBoxValue.Text);
}
catch (FormatException)
{
MessageBox.Show(messageCheckYourInput, wrongFormat, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textBoxValue.SelectAll();
return;
}
catch (OverflowException exception)
{
MessageBox.Show(exception.Message, wrongFormat, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textBoxValue.SelectAll();
return;
}
double fromCurrency = 1; //= (double)comboBoxFrom.SelectedValue;
double toCurrency = 0; // = (double)comboBoxTo.SelectedValue;
double result;
if (checkBoxOff.Checked == true)
{
fromCurrency = (double)comboBoxFrom.SelectedValue;
toCurrency = (double)comboBoxTo.SelectedValue;
}
else
{
// go online
Cursor.Current = Cursors.WaitCursor;
try
{
int indxFrom = comboBoxFrom.SelectedIndex;
int indxTo = comboBoxTo.SelectedIndex;
WebServiceX.CurrencyConvertor ws = new WebServiceX.CurrencyConvertor();
if (comboBoxFrom.Text == textBoxBase.Text)
fromCurrency = 1;
else
fromCurrency = ws.ConversionRate(getCurrency(textBoxBase.Text), getCurrency(comboBoxFrom.Text));
if (comboBoxTo.Text == textBoxBase.Text)
toCurrency = 1;
else
toCurrency = ws.ConversionRate(getCurrency(textBoxBase.Text), getCurrency(comboBoxTo.Text));
// actualize
comboBoxTo.SelectedValue = toCurrency;
comboBoxTo.SelectedIndex = indxTo;
comboBoxFrom.SelectedValue = fromCurrency;
comboBoxFrom.SelectedIndex = indxFrom;
// save changes
ds.WriteXml(currencyXmlFile);
}
catch (WebException)
{
MessageBox.Show(messageCheckNetwork, unavailableWS, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
catch (IOException exception)
{
MessageBox.Show(exception.Message, fileError, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
catch (SecurityException exception)
{
MessageBox.Show(exception.Message, applicationError, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
Cursor.Current = Cursors.Default;
}
result = from*(1/fromCurrency)*toCurrency;
textBoxResult.Text = Math.Round(result, 2).ToString();
}
textBoxValue.Focus();
}
private void buttonRemove_Click(object sender, System.EventArgs e)
{
if (textBoxBase.Text == listBoxCurr.Text)
{
textBoxBase.Text = "";
MessageBox.Show(messageBaseDeleted, baseDeleted, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
}
ds.Tables[CURRENCY].Rows.RemoveAt(listBoxCurr.SelectedIndex);
}
private void inputPanel1_EnabledChanged(object sender, System.EventArgs e)
{
int absoluteHeight = this.Bounds.Height;
tabControl1.Height = (inputPanel1.Enabled) ? absoluteHeight - inputPanel1.Bounds.Height : absoluteHeight;
//tabControl1.Height = (inputPanel1.Enabled) ? 272 - inputPanel1.Bounds.Height : 272;
}
private void comboBoxTo_SelectedValueChanged(object sender, System.EventArgs e)
{
if (tabControl1.SelectedIndex == 0)
textBoxValue.Focus();
}
private void tabControl1_GotFocus(object sender, System.EventArgs e)
{
if (tabControl1.SelectedIndex == 0)
{
inputPanel1.Enabled = true;
textBoxValue.Focus();
}
}
private void Form1_GotFocus(object sender, System.EventArgs e)
{
if (tabControl1.SelectedIndex == 0)
{
inputPanel1.Enabled = true;
textBoxValue.Focus();
}
else
inputPanel1.Enabled = false;
}
/// <summary>
/// The combo-box with all available currencies on Option-Page got focus
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBoxCurr_GotFocus(object sender, System.EventArgs e)
{
inputPanel1.Enabled = true;
}
private void buttonClear_Click(object sender, System.EventArgs e)
{
textBoxValue.Text = "";
textBoxResult.Text = "";
inputPanel1.Enabled = true;
textBoxValue.Focus();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -