WpfAPP/WpfAppgroup/MainWindow.xaml.cs
2025-05-06 10:29:19 +02:00

94 lines
2.4 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System; // Grundlegende Funktionen (wie Umrechnung, Zahlen usw.)
using System.Windows; // Alles für Fenster (WPF)
using System.Windows.Controls; // Alles für Buttons, TextBoxen, ComboBox usw.
namespace Waehrungsrechner // Unser Projektname
{
// Die Hauptklasse, die das Fenster (Window) verwaltet
public partial class MainWindow : Window
{
// Die Währungen
private const double EuroZuPfund = 0.85; // 1 Euro = 0.85 Pfund
private const double PfundZuEuro = 1.18; // 1 Pfund = 1.18 Euro
public MainWindow()
{
InitializeComponent(); // Diese Methode baut das Fenster und verbindet es mit dem XAML
}
// Diese Methode wird aufgerufen, wenn der Benutzer auf "Umrechnen" klickt
private void Umrechnen_Click(object sender, RoutedEventArgs e)
{
double betrag; // Hier speichern wir den eingegebenen Betrag (z.B. 100.00)
// Wenn istZahl = true wird die Zahl in "betrag" gespeichert
bool istZahl = double.TryParse(BetragTextBox.Text, out betrag);
if (istZahl) // wenn eeingegeben zahl gültig ist
{
// Hole den ausgewählten Text aus der ComboBox z.B."Euro zu Pfund"
//RichtungComboBox.SelectedItem = „Euro zu Pfund“ oder „Pfund zu Euro“
string richtung = ((ComboBoxItem)RichtungComboBox.SelectedItem).Content.ToString();
double ergebnis = 0; // Hier speichern wir das Ergebnis der Umrechnung, Der Computer rechnet dann den Betrag um (z.B. 100€ → 85£)
//Der Code fragt nach, ob der Benutzer „Euro zu Pfund“ oder „Pfund zu Euro“
if (richtung == "Euro zu Pfund")
{
ergebnis = betrag * EuroZuPfund; // Euro zu Pfund
ErgebnisLabel.Content = "Ergebnis: " + ergebnis.ToString("F2") + " £";
}
else
{
ergebnis = betrag * PfundZuEuro; // Pfund zu Euro
ErgebnisLabel.Content = "Ergebnis: " + ergebnis.ToString("F2") + " €";
}
}
else // bei ungültiger zahl kommt das hier
{
ErgebnisLabel.Content = "Ungültige Zahl!";
}
}
}
}