private void btn_CurrAdd_Click(object sender, RoutedEventArgs e) { btn_CurrAdd.IsEnabled = false; string addStatus = "ADDED"; bool isAddAvailable = true; Border border = new Border(); foreach (string existedCurrency in cbox_srcCrnc.Items) { if (tbox_NewCurrency.Text == existedCurrency) { isAddAvailable = false; border.Visibility = Visibility.Visible; addStatus = "ERROR"; break; } } if (isAddAvailable) { cbox_trgCrnc.Items.Add(tbox_NewCurrency.Text); cbox_srcCrnc.Items.Add(tbox_NewCurrency.Text); TXT_DB.SaveDataBase(DB_path, currencies); } else { border.Visibility = Visibility.Visible; } tbox_NewCurrency.TextChanged -= tbox_newCurrency_TextChanged; tbox_NewCurrency.Text = addStatus; var dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += delegate { tbox_NewCurrency.TextChanged += tbox_newCurrency_TextChanged; tbox_NewCurrency.Text = ""; dispatcherTimer.Stop(); }; dispatcherTimer.Interval = new TimeSpan(0, 0, 1); dispatcherTimer.Start(); }
public MainWindow() { InitializeComponent(); currconvAPI = new CurrconvAPI("5f1965af1bceb7361177"); srcAmount = double.Parse(tbox_srcCrncAmount.Text); trgAmount = double.Parse(tbox_trgCrncAmount.Text); srcRate = double.Parse(tbox_srcRate.Text); trgRate = double.Parse(tbox_trgRate.Text); tbox_srcCrncAmount.TextChanged += tbox_srcCrncAmount_TextChanged; tbox_trgCrncAmount.TextChanged += tbox_trgCrncAmount_TextChanged; tbox_srcRate.TextChanged += tbox_srcRate_TextChanged; tbox_trgRate.TextChanged += tbox_trgRate_TextChanged; tbox_NewCurrency.TextChanged += tbox_newCurrency_TextChanged; TextChangedEventHandler validate = delegate { ValidateTextBoxInput(); }; tbox_srcCrncAmount.TextChanged += validate; tbox_trgCrncAmount.TextChanged += validate; tbox_srcRate.TextChanged += validate; tbox_trgRate.TextChanged += validate; cbox_srcCrnc.SelectionChanged += cbox_SelectionChanged; cbox_trgCrnc.SelectionChanged += cbox_SelectionChanged; cbox_srcCrnc.SelectionChanged += cbox_srcCrnc_SelectionChanged; cbox_trgCrnc.SelectionChanged += cbox_trgCrnc_SelectionChanged; cbox_srcCrnc.GotFocus += cbox_GotFocus; cbox_trgCrnc.GotFocus += cbox_GotFocus; cbox_srcCrnc.LostFocus += cbox_LostFocus; cbox_trgCrnc.LostFocus += cbox_LostFocus; TXT_DB.DB_Load(DB_path, AddNewCrnc); cbox_srcCrnc.SelectedIndex = 0; SelectionChangedEventHandler currencyRateSetter = delegate { string srcCrnc = cbox_srcCrnc.SelectedItem as string; string trgCrnc = cbox_trgCrnc.SelectedItem as string; BackgroundWorker worker = new BackgroundWorker(); tbox_srcRate.Text = "..."; tbox_trgRate.Text = "..."; worker.DoWork += (sender, args) => { args.Result = currconvAPI.GetExchangeRate(srcCrnc, trgCrnc); }; worker.RunWorkerCompleted += (sender, args) => { tbox_srcRate.Text = string.Format("{0:0.00##}", args.Result); }; worker.RunWorkerAsync(); }; cbox_srcCrnc.SelectionChanged += currencyRateSetter; cbox_trgCrnc.SelectionChanged += currencyRateSetter; cbox_trgCrnc.SelectedIndex = 1; }