Laden...

ComboBox = ComboBox if Statement

Erstellt von Chipsland vor 8 Jahren Letzter Beitrag vor 8 Jahren 2.352 Views
Thema geschlossen
C
Chipsland Themenstarter:in
2 Beiträge seit 2015
vor 8 Jahren
ComboBox = ComboBox if Statement

Hallo alle zusammen,
heute bin ich bei meinem Währungsrechner wieder einmal auf Kriegsfuß. Ich möchte das, wenn der gleiche Text ausgewählt wurde in den Comboboxen das dann eine MessageBox kommt, und wenn in einem der beiden nichts ausgewählt wurde möchte ich auch das MessageBox kommt. Es wird mir zwar keiner Fehler angezeigt, doch wenn ich probiere ob das nun funktioniert sagt er mir bei :

double Waehrung1 = Convert.ToDouble(textBox1.Text);

"FormatException wurde nicht behandelt. Die Eingabezeichenfolge hat das falsche Format"

Doch ich weiß noch nicht mal was Waerung1 mit den ComboBoxen zu tun hat
Hier mein ganzer Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Währungsrechner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
            //Waehrung1 und Ergebnis deklariert
            double Waehrung1 = Convert.ToDouble(textBox1.Text);
            double Ergebnis;



            //Wenn zweimal die selbe Währung ausgewählt wurde zeigt er eine MessageBox
            if (comboBox1.SelectedItem.ToString() == comboBox2.SelectedItem.ToString())
            {
                MessageBox.Show("Sie haben zweimal die gleiche Währung ausgewählt");
            }




            //Wenn ComboBox2 leer ist wird eine MessageBox angezeigt
            if (comboBox2.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Sie haben keine Währung ausgewählt");
            }



            //Wenn ComboBox1 leer ist wird eine MessageBox angezeigt
            if (comboBox1.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Sie haben keine Währung ausgewählt");
            }
            
            //Wenn Euro ausgewählt wurde rechnet er mit dem Wert dem ihm in ComboBox2 zugeteilt wurde
            if (comboBox1.SelectedItem.ToString() == "Euro")
            {
                if (comboBox2.SelectedItem.ToString() == "Dollar")
                {
                    Ergebnis = Waehrung1 * 1.1304;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Yen")
                {
                    Ergebnis = Waehrung1 * 7.0178;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "JP Yen")
                {
                    Ergebnis = Waehrung1 * 138.8240;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Russischer Rubel")
                {
                    Ergebnis = Waehrung1 * 61.5894;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "CA Dollar")
                {
                    Ergebnis = Waehrung1 * 1.3870;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
            }


            //Gleicher Vorgang wie vorher (unterschiedlicher Wechselkurs)
            if (comboBox1.SelectedItem.ToString() == "Dollar")
            {
                if (comboBox2.SelectedItem.ToString() == "Euro")
                {
                    Ergebnis = Waehrung1 * 0.8877;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Yen")
                {
                    Ergebnis = Waehrung1 * 6.2065;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "JP Yen")
                {
                    Ergebnis = Waehrung1 * 123.4111;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Russischer Rubel")
                {
                    Ergebnis = Waehrung1 * 54.5583502;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "CA Dollar")
                {
                    Ergebnis = Waehrung1 * 1.2279;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
            }


            //Gleicher Vorgang wie vorher (unterschiedlicher Wechselkurs)
            if (comboBox1.SelectedItem.ToString() == "Yen")
            {
                if (comboBox2.SelectedItem.ToString() == "Euro")
                {
                    Ergebnis = Waehrung1 * 0.1430;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Dollar")
                {
                    Ergebnis = Waehrung1 * 0.1611;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "JP Yen")
                {
                    Ergebnis = Waehrung1 * 19.8841;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Russischer Rubel")
                {
                    Ergebnis = Waehrung1 * 8.7905;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "CA Dollar")
                {
                    Ergebnis = Waehrung1 * 0.1978;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
            }


            //Gleicher Vorgang wie vorher (unterschiedlicher Wechselkurs)
            if (comboBox1.SelectedItem.ToString() == "JP Yen")
            {
                if (comboBox2.SelectedItem.ToString() == "Euro")
                {
                    Ergebnis = Waehrung1 * 0.0072;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Dollar")
                {
                    Ergebnis = Waehrung1 * 0.0081;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Yen")
                {
                    Ergebnis = Waehrung1 * 0.0503;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Russischer Rubel")
                {
                    Ergebnis = Waehrung1 * 0.4421;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "CA Dollar")
                {
                    Ergebnis = Waehrung1 * 0.0099;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
            }


            //Gleicher Vorgang wie vorher (unterschiedlicher Wechselkurs)
            if (comboBox1.SelectedItem.ToString() == "Russischer Rubel")
            {
                if (comboBox2.SelectedItem.ToString() == "Euro")
                {
                    Ergebnis = Waehrung1 * 0.0163;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Dollar")
                {
                    Ergebnis = Waehrung1 * 0.0183;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Yen")
                {
                    Ergebnis = Waehrung1 * 0.1138;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "JP Yen")
                {
                    Ergebnis = Waehrung1 * 2.2620;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "CA Dollar")
                {
                    Ergebnis = Waehrung1 * 0.0225;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
            }


            //Gleicher Vorgang wie vorher (unterschiedlicher Wechselkurs)
            if (comboBox1.SelectedItem.ToString() == "CA Dollar")
            {
                if (comboBox2.SelectedItem.ToString() == "Euro")
                {
                    Ergebnis = Waehrung1 * 0.7229;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Dollar")
                {
                    Ergebnis = Waehrung1 * 0.8144;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Yen")
                {
                    Ergebnis = Waehrung1 * 5.0546;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "JP Yen")
                {
                    Ergebnis = Waehrung1 * 100.5071;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
                else if (comboBox2.SelectedItem.ToString() == "Russischer Rubel")
                {
                    Ergebnis = Waehrung1 * 44.4328;
                    textBox2.Text = Convert.ToString(Ergebnis);
                }
            }

        }
    }
}

Würde mich über schnelle Hilfe freuen.

MfG
Chipsland

P.S.: bin noch ziemlich neu beim programmieren mit Windows Forms 😁

D
615 Beiträge seit 2009
vor 8 Jahren

Hallo Chipsland

Ich habe mir Deine Spaghettis mal angeschaut 😃

C#-Code:
double Waehrung1 = Convert.ToDouble(textBox1.Text);

"FormatException wurde nicht behandelt. Die Eingabezeichenfolge hat das falsche Format"

In der Hilfe steht doch alles dazu:

Convert.ToDouble(string)

=> FormatException bedeutet, der string wurde nicht als "Double" erkannt. Die hat warsch. mit CultureSettings zutun. (Culture Invariant)

Als Tipp:
=> Ich würde es eher etwa so aufbauen:
BasisKlasse Währung
Alle Währungen erben davon (jede soll eine Klasse sein) und überschreiben bspw. den WechselKurs. Dein Code würde sich dann auf etwa 2 Zeile reduzieren àlà
doubleEingabe * (comboSelectedItem as Waehrung).Kurs.

Zudem könntest du dann später sehr einfach die aktuellen Kurse einfach von EZB oder ähnlichem laden usw...

Schau Dir ggf. mal Vererbung an.

Ich hoff diese Beitrag kommt noch durch bevor der Thread wegen "Absoluten Grundlagen" geschlossen wird.

Schönes Weekend

Diräkt

16.835 Beiträge seit 2008
vor 8 Jahren

So läufts hier nicht.
Eigentlich inhaltlich genau die gleiche Frage wie WinForms: Ergebnis einer Währungsrechnung in Textbox schreiben

Grundlagen schön und gut; aber wir müssen Dir keine Fehlermeldungen erklären, wenn die mehr als eindeutig und in der offiziellen Dokumentation bereits ausführlich beschrieben und mit einem Beispiel versehen erläutert werden.

Daher ist hier zu.

Thema geschlossen