Entwickler-Ecke

WinForms - Mehrere ComboBoxen -> Fehler bei der Abfrage der Ergebnisse


KRS - Do 01.11.12 22:26
Titel: Mehrere ComboBoxen -> Fehler bei der Abfrage der Ergebnisse
Hallo,

ich bin neu hier und habe auch schon gleich eine Frage. Seit ein paar Tagen beschäftige ich mit mit C# und versuch ein Zusatzmodul für AutoCad zu erstellen. Dafür habe ich ein Form erstellt, in das der Anwender ein paar Zahlenwerte eingeben und in mehreren ComboBoxen etwas wählen muss.

Inhalt der comboBoxen:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
 
        public Form1()
        {
            InitializeComponent();
            //Compobox1 füllen (firsttyp)
            Dictionary<string, string> firsttyp;
            firsttyp = new Dictionary<string, string>();
            firsttyp.Add("0", "Latte");
            firsttyp.Add("1", "Bohle");
            firsttyp.Add("2", "Pfette");
            BindingSource myBindingSource = new BindingSource();
            myBindingSource.DataSource = firsttyp;
            comboBox1.DataSource = myBindingSource;
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember = "Key";

            //Compobox2 füllen (Firstpunktausbildung)
            Dictionary<string, string> firstpunktausbildung;
            firstpunktausbildung = new Dictionary<string, string>();
            firstpunktausbildung.Add("0", "Stumpfstoß");
            firstpunktausbildung.Add("1", "Verblattung");
            BindingSource myBindingSource2= new BindingSource();
            myBindingSource2.DataSource = firstpunktausbildung;
            comboBox2.DataSource = myBindingSource2;
            comboBox2.DisplayMember = "Value";
            comboBox2.ValueMember = "Key";

            //Compobox4 füllen (Verbindungsmittel)
            Dictionary<string, string> firstpunktausbildungart;
            firstpunktausbildungart = new Dictionary<string, string>();
            firstpunktausbildungart.Add("0", "Nägel");
            firstpunktausbildungart.Add("1", "Dübel");
            firstpunktausbildungart.Add("2", "Bolzen");
            BindingSource myBindingSource3 = new BindingSource();
            myBindingSource3.DataSource = firstpunktausbildungart;
            comboBox4.DataSource = myBindingSource3;
            comboBox4.DisplayMember = "Value";
            comboBox4.ValueMember = "Key";
        }


Und so werte ich die comboBox1 aus:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         //Pfette
            if (comboBox1.SelectedIndex==2)
            {
                this.label54.Text = "Pfettenabmessungen";
                this.label53.Text = "Pfetten Höhe";
                this.label52.Text = "Pfetten Breite";
                //Textbox für t freigeben
                this.textBox17.Enabled = true;
                //Firstausbildungstyp aktivieren
                this.comboBox2.Enabled = true;
                this.comboBox4.Enabled = true;
                this.comboBox5.Enabled = true;
                this.comboBox6.Enabled = true;
                this.comboBox7.Enabled = true;
                this.comboBox8.Enabled = true;
                this.checkBox1.Enabled = true;
            }
            //Latte
            else if (comboBox1.SelectedIndex==0)
            {
                this.label54.Text = "Lattenabmessungen";
                this.label53.Text = "Latte Höhe";
                this.label52.Text = "Latte Breite";
                //Textbox für t sperren 
                this.textBox17.Enabled = false;
                //Firstausbildungstyp aktivieren
                this.comboBox2.Enabled = true;
                this.comboBox4.Enabled = true;
                this.comboBox5.Enabled = true;
                this.comboBox6.Enabled = true;
                this.comboBox7.Enabled = true;
                this.comboBox8.Enabled = true;
                this.checkBox1.Enabled = true;
            }
            //Bohle
            else if (comboBox1.SelectedIndex==1)
            {
                this.label54.Text = "Bohlenabmessungen";
                this.label53.Text = "Bohlen Höhe";
                this.label52.Text = "Bohlen Breite";
                //Textbox für t sperren 
                this.textBox17.Enabled = false;
                //Firstausbildungstyp sperren
                this.comboBox2.Enabled = false;
                this.comboBox4.Enabled = false;
                this.comboBox5.Enabled = false;
                this.comboBox6.Enabled = false;
                this.comboBox7.Enabled = false;
                this.comboBox8.Enabled = false;
                this.checkBox1.Enabled = false;
            }
        }

Dieser Teil funktioniert auch. Daher habe ich die Auswertung der comboBox2 analog versuch. Leider ohne Erfolg. Viellicht könnt ihr mir ja helfen.


Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
     private void comboBox2_Changed(object sender, EventArgs e)
     {   
         MessageBox.Show("er hat gemerkt das sich bei 2 was ändert "); //Versuch um zu Prüfen, ob bei Änderung was passiert, leider ohne Erfolg
         if (comboBox2.SelectedIndex == 0)
         {
             this.comboBox4.Enabled = false;
             this.comboBox5.Enabled = false;
             this.comboBox6.Enabled = false;
             this.comboBox7.Enabled = false;
             this.comboBox8.Enabled = false;
             this.checkBox1.Enabled = false;
         }
         else if (this.comboBox2.SelectedIndex == 1)
         {
             this.comboBox4.Enabled = true;
             this.comboBox5.Enabled = true;
             this.comboBox6.Enabled = true;
             this.comboBox7.Enabled = true;
             this.comboBox8.Enabled = true;
             this.checkBox1.Enabled = true;
         }
    }


Wäre sehr dankbar für ein paar Tipps.
Vielen Dank schon mal

Mfg

Moderiert von user profile iconTh69: Titel sowie Codeformatierung leicht überarbeitet.


Th69 - Fr 02.11.12 12:14

Hallo KRS :welcome:

was genau funktioniert denn bei der 2. ComboBox nicht bzw. welchen Fehler erhältst du?


KRS - Fr 02.11.12 12:48

Hallo Th69,

also Fehler beim Ausführen bekomme ich keinen und in der ComboBox2 werden auch Stumpfstoß und Verblattung als auswahlpunkte angezeigt.
Leider Funktionier die Auswertung der comboBox2 garnicht. Ich habe daher eine Messagebox in die private void comboBox2_Changed(object sender, EventArgs e) getan um zu prüfen ob hier auf den Code überhaupt zugegriffen wird. Leider wohl nicht, da die Messagebox nicht erscheint.

Daher die Frage: Wo ist mein Fehler beim abfragen der Ergebnisse von der comboBox2?

Gruß


Th69 - Fr 02.11.12 13:00

Hallo,

dann hast du wohl den Eventhandler für die 2. ComboBox nicht abonniert. Schau mal im Forms-Designer nach (Eigenschaften / Ereignisse [der gelbe Blitz]).
Wenn du dann die 'comboBox2' auswählst, sollte bei 'SelectedIndexChanged' deine Eventmethode comboBox2_Changed stehen. Ich nehme an, du hast einfach nur im Code die Eventmethode definiert (so kann das natürlich nicht funktionieren ;-)).


KRS - Fr 02.11.12 17:38

Super,

vielen, vielen Dank. Ich habe jetzt 3 Tage daran rumgebastelt und dabei war es so einfach (wenn man’s weiß!).

Gruß


Ralf Jansen - Fr 02.11.12 17:43

Zitat:
(Eigenschaften / Ereignisse [der gelbe Blitz]).

In VS2012 sind Signalfarben nur noch 'ne schöne Erinnerung :cry: