hipzi

MainWindow.xaml.cs

Apr 6th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19.  
  20. namespace CurrencyConverter_Static
  21. {
  22.     public partial class MainWindow : Window
  23.     {
  24.         public MainWindow()
  25.         {
  26.             InitializeComponent();
  27.             BindCurrency();
  28.         }
  29.  
  30.         private double getRate(string fromCurrency, string toCurrency)
  31.         {
  32.             var json = "";
  33.             double rate;
  34.             try
  35.             {
  36.                 string url = string.Format("https://free.currconv.com/api/v7/convert?q={0}_{1}&compact=ultra&apiKey=cd0304f3d4c6b7e923d3", fromCurrency.ToUpper(), toCurrency.ToUpper());
  37.                 string key = string.Format("{0}_{1}", fromCurrency.ToUpper(), toCurrency.ToUpper());
  38.  
  39.                 json = new WebClient().DownloadString(url);
  40.                 dynamic stuff = JsonConvert.DeserializeObject(json);
  41.                 rate = stuff[key];
  42.             }
  43.             catch
  44.             {
  45.                 rate = 0;
  46.             }
  47.             return rate;
  48.         }
  49.  
  50.         private void BindCurrency()
  51.         {
  52.             DataTable dtCurrency = new DataTable();
  53.             dtCurrency.Columns.Add("Text");
  54.  
  55.             dtCurrency.Rows.Add("--SELECT--");
  56.             dtCurrency.Rows.Add("IDR");
  57.             dtCurrency.Rows.Add("INR");
  58.             dtCurrency.Rows.Add("USD");
  59.             dtCurrency.Rows.Add("EUR");
  60.             dtCurrency.Rows.Add("SAR");
  61.             dtCurrency.Rows.Add("BAM");
  62.             dtCurrency.Rows.Add("PYG");
  63.             dtCurrency.Rows.Add("TRY");
  64.             dtCurrency.Rows.Add("JPY");
  65.  
  66.             cmbFromCurrency.ItemsSource = dtCurrency.DefaultView;
  67.             cmbFromCurrency.DisplayMemberPath = "Text";
  68.             cmbFromCurrency.SelectedIndex = 0;
  69.  
  70.             cmbToCurrency.ItemsSource = dtCurrency.DefaultView;
  71.             cmbToCurrency.DisplayMemberPath = "Text";
  72.             cmbToCurrency.SelectedIndex = 0;
  73.         }
  74.  
  75.         private void Convert_Click(object sender, RoutedEventArgs e)
  76.         {
  77.             double ConvertedValue;
  78.  
  79.             if (txtCurrency.Text == null || txtCurrency.Text.Trim() == "")
  80.             {
  81.                 MessageBox.Show("Please Enter Currency", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
  82.                 txtCurrency.Focus();
  83.                 return;
  84.             }
  85.  
  86.             if (cmbFromCurrency.Text == cmbToCurrency.Text)
  87.             {
  88.                 ConvertedValue = double.Parse(txtCurrency.Text);
  89.                 lblCurrency.Content = cmbToCurrency.Text + " " + ConvertedValue.ToString("N3");
  90.             }
  91.             else
  92.             {
  93.                 double rate = getRate(cmbFromCurrency.Text, cmbToCurrency.Text);
  94.                 ConvertedValue = (rate * double.Parse(txtCurrency.Text));
  95.                 lblCurrency.Content = cmbToCurrency.Text + " " + ConvertedValue.ToString("N3");
  96.             }
  97.         }
  98.  
  99.         private void Clear_Click(object sender, RoutedEventArgs e)
  100.         {
  101.             ClearControls();
  102.         }
  103.  
  104.         private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
  105.         {
  106.             Regex regex = new Regex("[^0-9]+");
  107.             e.Handled = regex.IsMatch(e.Text);
  108.         }
  109.  
  110.         private void ClearControls()
  111.         {
  112.             txtCurrency.Text = string.Empty;
  113.             if (cmbFromCurrency.Items.Count > 0)
  114.                 cmbFromCurrency.SelectedIndex = 0;
  115.             if (cmbToCurrency.Items.Count > 0)
  116.                 cmbToCurrency.SelectedIndex = 0;
  117.             lblCurrency.Content = "";
  118.             txtCurrency.Focus();
  119.         }
  120.  
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment