private void equalsButton_Click(object sender, EventArgs e) { double my_amount = 0; double converted_amount = 0; if (double.TryParse(userInput, out my_amount) == true) { if (my_amount > 0) { converted_amount = Currency_Exchange_Class.CurrencyConversion( (Currencies)myCurrencyBox.SelectedIndex, (Currencies)convertCurrencyBox.SelectedIndex, my_amount); convertAmountText.Text = converted_amount.ToString(); myAmountLabel.Enabled = false; myAmountText.Enabled = false; convertAmountCode.Visible = true; repeatConversionGroup.Visible = true; } else { MessageBox.Show("You entered a negative number", "Negative Number", MessageBoxButtons.OK, MessageBoxIcon.Error); myAmountText.Clear(); } } else { MessageBox.Show("Please enter a number greater than zero", "Non-Numerical Value", MessageBoxButtons.OK, MessageBoxIcon.Error); myAmountText.Clear(); } }
}//end EnableEqualsButton /// <summary> /// /// This method has 2 double variables. /// /// 1. input: this stores the value that the user /// types in the HaveInput box. /// 2. result: result is the variable that stores /// the calculated number in Currency Exchange Class.cs /// which has a method returning value. /// /// This method also has if functions which checks if /// the user input is a number or a negative number, /// in which case an error message appears and prompts /// the user to enter a valid input. /// /// The method also makes the ToCurrency label visible, /// AmountIWant label enabled, formats the result to 4 decimals, /// and enables the AnotherConversion group box. /// /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EqualsButton_Click(object sender, EventArgs e) { double input, result; if (!double.TryParse(HaveInput.Text, out input)) { MessageBox.Show("Value entered must be numeric"); HaveInput.Text = ""; HaveInput.Focus(); return; } else if (input < 0) { MessageBox.Show("Value entered must be a valid number"); HaveInput.Text = ""; return; } result = Currency_Exchange_Class.Convert_Currencies(input, whichConversion, whichConversionIWant); ToCurrency.Visible = true; AmountIWant.Enabled = true; WantInput.Text = result.ToString("0.####"); EnableGroupBox(); } //end of EqualsButton_Click
public Currency() { InitializeComponent(); HaveComboBox.DataSource = Currency_Exchange_Class.InitialiseComboBox(); WantComboBox.DataSource = Currency_Exchange_Class.InitialiseComboBox(); }//end Form1
public Form1() { InitializeComponent(); myCurrencyBox.DataSource = Currency_Exchange_Class.InitialiseComboBox(); convertCurrencyBox.DataSource = Currency_Exchange_Class.InitialiseComboBox(); myCurrencyBox.SelectedIndexChanged += new EventHandler(myCurrencyBox_SelectedIndexChanged); convertCurrencyBox.SelectedIndexChanged += new EventHandler(convertCurrencyBox_SelectedIndexChanged); DisableControls(); }//end Form1
/// <summary> /// Initializes the Currency Exchange form. /// Adds the Currency Exchange Class data for the combo boxes /// Adds the combo boxes event handerlers /// </summary> public Form1() { InitializeComponent(); originalCurrencyComboBox.DataSource = Currency_Exchange_Class.InitialiseComboBox(); newCurrencyComboBox.DataSource = Currency_Exchange_Class.InitialiseComboBox(); originalCurrencyComboBox.SelectedIndexChanged += new EventHandler(CurrencyHaveComboBox_SelectedIndexChanged); newCurrencyComboBox.SelectedIndexChanged += new EventHandler(CurrencyWantComboBox_SelectedIndexChanged); }//end Form1
}//end Form1() private void cboCurrencyHave_SelectedIndexChanged(object sender, EventArgs e) { // If a selection has been made from the cbo if (cboCurrencyHave.Text != "") { cboCurrencyWant.Enabled = true; cboCurrencyHave.Enabled = false; // Set currency have code label based off selected currency lblCurrencyCodeHave.Text = Currency_Exchange_Class.GetCurrencyCode(cboCurrencyHave.SelectedIndex); } } // end cboCurrencyHave_SelectedIndexChanged()
public Form1() { InitializeComponent(); // Inserted according to assignement specifications cboCurrencyHave.DataSource = Currency_Exchange_Class.InitialiseComboBox(); cboCurrencyWant.DataSource = Currency_Exchange_Class.InitialiseComboBox(); // Inserted according to assignement specifications cboCurrencyHave.SelectedIndexChanged += new EventHandler(cboCurrencyHave_SelectedIndexChanged); cboCurrencyWant.SelectedIndexChanged += new EventHandler(cboCurrencyWant_SelectedIndexChanged); }//end Form1()
public Form1() { InitializeComponent(); comboBoxCurrencyOld.DataSource = Currency_Exchange_Class.InitialiseComboBox(); comboBoxCurrencyNew.DataSource = Currency_Exchange_Class.InitialiseComboBox(); comboBoxCurrencyOld.SelectedIndexChanged += new EventHandler(comboBoxCurrencyOld_SelectedIndexChanged); comboBoxCurrencyNew.SelectedIndexChanged += new EventHandler(comboBoxCurrencyNew_SelectedIndexChanged); //For some reason, I need to disable everything at the start after i put the above in //because it will just enable the whole form? Not sure if this was intended or if i stuffed something up comboBoxCurrencyOld.Enabled = true; comboBoxCurrencyNew.Enabled = false; labelNewCurrency.Visible = false; labelOldCurrency.Visible = false; buttonCalculate.Enabled = false; textBoxCurrentAmount.Enabled = false; groupBoxReset.Visible = false; }//end Form1
}//end CurrencyTextBox_TextChanged /// <summary> /// This method starts off by determining if the entered number in the CurrencyTextBox /// is a number. If not, a message box is shown, telling them what is wrong, then clears the CurrencyTextBox /// and focuses it for the user and disables the button. /// If it is a number, it saves the number into original money. /// The program then grabs the selected indexs for the originalCurrencyComboBox and /// newCurrencyComboBox. These values are then passed through a method in the /// Currency_Exchange_Class which converts the currency the user has chosen. /// The wanted currency enum name is passed into the wantType label and displayed for the user. /// The converted money is then passed into the ConvertedCurrencyTextBox to be displayed for the user. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void equalsButton_Click(object sender, EventArgs e) { int originalType, newType; double originalMoney, newMoney; if (!double.TryParse(CurrencyTextBox.Text, out originalMoney)) { if (originalMoney < 1) { MessageBox.Show("Please only use numbers"); CurrencyTextBox.Text = ""; CurrencyTextBox.Focus(); equalsButton.Enabled = false; } else { originalType = originalCurrencyComboBox.SelectedIndex; newType = newCurrencyComboBox.SelectedIndex; originalMoney = double.Parse(CurrencyTextBox.Text); newMoney = Currency_Exchange_Class.conversion(originalType, newType, originalMoney); Currencies newCountry = (Currencies)newType; wantType.Text = newCountry.ToString(); ConvertedCurrencyTextBox.Enabled = true; ConvertedCurrencyTextBox.Text = newMoney.ToString(); ConvertedCurrencyTextBox.Enabled = false; CurrencyTextBox.Enabled = false; AnotherConversionGroupBox.Visible = true; } } else { MessageBox.Show("Please use a number greater than 0"); CurrencyTextBox.Text = ""; CurrencyTextBox.Focus(); equalsButton.Enabled = false; } }// end equalsButton_Click
} // end txtAmountHave_TextChanged() private void btnEquals_Click(object sender, EventArgs e) { // Call PerformCurrencyConversion to convert amount according to currency provided txtAmountWant.Text = Currency_Exchange_Class.PerformCurrencyConversion(cboCurrencyHave.SelectedIndex, cboCurrencyWant.SelectedIndex, txtAmountHave.Text).ToString("0.####"); // Lock input in amount have field from being changed txtAmountHave.Enabled = false; // Make equals button unclickable btnEquals.Enabled = false; // Show currency wanted currency code lblCurrencyCodeWant.Visible = true; // Show another conversion options grpConversion.Visible = true; } // end btnEquals_Click()
} // End comboBox_CurrencyWant_SelectedIndexChanged() /// <summary> /// When the user clicks the button, convert and display currency according to the chosen currencies. /// Then display end of program options. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Equals_Click(object sender, EventArgs e) { // Convert Currency currencyWantConverted = Currency_Exchange_Class.ConvertCurrency(currencyHave_index, currencyWant_index, richTextBox_AmountHave.Text); // If the convertCurrency succeeded if (currencyWantConverted >= 0) { // Display the converted amount then disable amount input and convert button richTextBox_AmountWant.Text = currencyWantConverted.ToString(); richTextBox_AmountHave.Enabled = false; Button_Equals.Enabled = false; // Display the end of program options GroupBox_RadioButtons_Retry.Enabled = true; GroupBox_RadioButtons_Retry.Visible = true; } else { // If the convertCurrency failed, show the user an error message DialogResult ValueError = MessageBox.Show("Please input positive numbers only.", "Input Error", MessageBoxButtons.RetryCancel); } } // End Button_Equals_Click()
}//End output private void buttonCalculate_Click(object sender, EventArgs e) { double currencyCurrent, result; //Set variables needed for conversion //Check if user input is a integer, if not, reset the input if (!double.TryParse(textBoxCurrentAmount.Text, out currencyCurrent)) { MessageBox.Show("Value entered must be numeric"); textBoxCurrentAmount.Text = ""; textBoxCurrentAmount.Focus(); return; }//End number check //Check for positive if (double.TryParse(textBoxCurrentAmount.Text, out currencyCurrent)) { if (currencyCurrent < 0) { MessageBox.Show("Value entered must be positive"); textBoxCurrentAmount.Text = ""; textBoxCurrentAmount.Focus(); return; } }//end positive check //Selecting the currency I am converting too in a switch based on the enum Currencies switch (whichCurrencyToo) { case Currencies.AUD: //If the currency we want is 1 (AUD), set the cValue to equal the currency we have int cValue = whichConversionFrom; //set the result to equal the return value from the conversion, rounded using Math.Round, to 2 places, rounding from zero result = Math.Round(Currency_Exchange_Class.ConvertToAustralia(currencyCurrent, cValue), 2, MidpointRounding.AwayFromZero); //send the result to the output method EnableOutput(result); //If we got to this, break out of the switch break; case Currencies.CNY: cValue = whichConversionFrom; //The nvalue is set to equal the enum value of the currency we want int nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToChina(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.DKK: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToDenmark(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.EUR: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToEurope(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.INR: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToIndia(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.NZD: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToNewZealand(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.AED: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToUAE(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.GBP: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToUK(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.USD: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToUSA(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; case Currencies.VND: cValue = whichConversionFrom; nValue = whichConversionToo; result = Math.Round(Currency_Exchange_Class.ConvertToVietnam(currencyCurrent, cValue, nValue), 2, MidpointRounding.AwayFromZero); EnableOutput(result); break; } //End Switch } //End Button Calculate
}//end Form1 /// <summary> /// Load the form and initialise the comboBoxes /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { // For each combo box, initialise it comboBox_CurrencyHave.Items.AddRange(Currency_Exchange_Class.InitialiseComboBox()); comboBox_CurrencyWant.Items.AddRange(Currency_Exchange_Class.InitialiseComboBox()); } // End Form1_Load()