using Newtonsoft.Json; using System.Collections.Generic; namespace CurrencyConvert { public partial class MoneyConverter : Form { private readonly string[] supportedCurrencies = new string[] { "AUD", "BGN", "BRL", "CAD", "CHF", "CNY", "CZK", "DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "ISK", "JPY", "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PLN", "RON", "SEK", "SGD", "THB", "TRY", "USD", "ZAR" }; public MoneyConverter() { InitializeComponent(); comboBox1.Items.Clear(); comboBox2.Items.Clear(); foreach (string currency in supportedCurrencies) { comboBox1.Items.Add(currency); comboBox2.Items.Add(currency); } comboBox1.SelectedIndex = comboBox1.Items.IndexOf("EUR"); comboBox2.SelectedIndex = comboBox2.Items.IndexOf("USD"); label4.AutoSize = true; label4.Font = new Font(label4.Font.FontFamily, 12, FontStyle.Bold); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)Keys.Back) { e.Handled = true; } } private async void button1_Click(object sender, EventArgs e) { label4.Text = "Berechne..."; button1.Enabled = false; if (string.IsNullOrWhiteSpace(textBox1.Text)) { MessageBox.Show("Bitte geben Sie einen Betrag ein."); button1.Enabled = true; return; } string amountText = textBox1.Text.Replace(',', '.'); double amount; bool isNumber = double.TryParse(amountText, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out amount); if (!isNumber) { MessageBox.Show("Bitte geben Sie einen gültigen Betrag ein."); button1.Enabled = true; return; } if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null) { MessageBox.Show("Bitte wählen Sie beide Währungen aus."); button1.Enabled = true; return; } string fromCurrency = comboBox1.SelectedItem.ToString().Trim().ToUpper(); string toCurrency = comboBox2.SelectedItem.ToString().Trim().ToUpper(); bool fromSupported = false; bool toSupported = false; foreach (string currency in supportedCurrencies) { if (currency == fromCurrency) fromSupported = true; if (currency == toCurrency) toSupported = true; } if (!fromSupported || !toSupported) { MessageBox.Show("Eine oder beide Währungen werden nicht unterstützt."); button1.Enabled = true; return; } string url = "https://api.frankfurter.app/latest?amount=" + amountText + "&from=" + fromCurrency + "&to=" + toCurrency; HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(url); string json = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { MessageBox.Show("Fehler beim Abrufen der Wechselkurse."); button1.Enabled = true; return; } FrankfurterResponse data = JsonConvert.DeserializeObject(json); if (data == null || data.rates == null || !data.rates.ContainsKey(toCurrency)) { MessageBox.Show("Fehler beim Abrufen der Wechselkurse."); button1.Enabled = true; return; } double result = data.rates[toCurrency]; label4.Text = amount + " " + fromCurrency + " = " + result.ToString("0.00") + " " + toCurrency; button1.Enabled = true; } private void Form1_Load(object sender, EventArgs e) { } private void label3_Click(object sender, EventArgs e) { } } public class FrankfurterResponse { public double amount { get; set; } public string base_currency { get; set; } public string date { get; set; } public Dictionary rates { get; set; } } }