Autor Beitrag
aloneboy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: So 11.01.15 20:29 
Hallo zusammen,

ich brauche mal eure Hilfe und weiss einfach nicht wie ich das bewerkstelligen soll.

Es sind 5 ComboxBoxen auf einer Form platziert und mit den selben Einträgen gefüllt.
Nachdem die Auswahl der Comboboxeinträge stattgefunden hat, soll beim click eines Buttons geprüft werden ob evtl. Einträge doppelt ausgewählt wurden, das sollte verhindert werden, doch wie mache ich das?

Danke schon mal in vorab für Eure Hilfe
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 11.01.15 20:37 
Hallo,

das m.E. einfachste ist es, eine doppelte Schleife zu verwenden, und jede ComboBox mit einer anderen zu vergleichen (außer natürlich mit sich selbst). Dazu am besten die 5 Comboboxen in eine Liste packen.
aloneboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: So 11.01.15 20:44 
öhmmm, ja

in eine Liste packen???
aloneboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: So 11.01.15 21:26 
um ein Bsp. wäre ich sehr dankbar!
C#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: So 11.01.15 22:10 
Hallo,

Was Th69 meint ist folgendes:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
        void Compare()
        {
            List<ComboBox> boxes = Controls.OfType<ComboBox>().ToList();

            for (int i = 0; i < boxes.Count; i++)
            {
                for (int j = 0; j < boxes.Count; j++)
                {
                    if (i == j) continue;

                    if (boxes[i].SelectedItem == boxes[j].SelectedItem)
                    {
                        // Gleiche Einträge wurden gewählt
                    }
                }
            }
        }

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler
aloneboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: So 11.01.15 22:30 
Hallo und VIELEN Dank!!!!

Habe da noch eine Frage dazu, rein informativ

was ist wenn auf dem Formular mehr ComboBoxen sind als ausgewertet werden soll?
Und was ist, wenn bei einer Combobxo o. auch mehrere nichts ausgewählt wurde? dann funktioniert es mit SelectedItem nicht?

Gruß
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 12.01.15 04:52 
- Nachträglich durch die Entwickler-Ecke gelöscht -
aloneboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: Mo 12.01.15 08:02 
Ok, danke
C#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Mo 12.01.15 09:28 
Hallo,

du kannst die Liste auch anhand bestimmter kriterien filtern.
Ändere diese Zeile
ausblenden C#-Quelltext
1:
List<ComboBox> boxes = Controls.OfType<ComboBox>().ToList();					

wie folgt ab
ausblenden C#-Quelltext
1:
List<ComboBox> boxes = Controls.OfType<ComboBox>().Where(comboBox => comboBox.Name.Contains("ComboBox") || comboBox.Enabled).ToList();					

In die Linq-Methode Where(..) kannst du dann beliebige Bedingungen reinschreiben.

Wenn du noch ComboBoxen in Unterelementen (z.B. Panels) finden willst, musst du eine rekursive Suche machen.

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler
Greenberet
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 339
Erhaltene Danke: 20

Win 10
C# (VS 2012), C++ (VS 2012/GCC), PAWN(Notepad++), Java(NetBeans)
BeitragVerfasst: Do 15.01.15 11:14 
die Doppelte For Schleife ist hier leider nicht gerade sehr effizient :P

j müsste mit i+1 Anfangen:

  • Boxen mit j<i wurden ja bereits schon überprüft, also wodurch die Arbeit noch einmal machen?
  • if (i == j) continue; fällt damit weg


Alternativ könntest du auch eine foreach Schleife über die ComboBoxen machen und das Ergebnis in einer Liste speichern.
Vor dem Speichern überprüfst du ob das Item schon existiert und falls ja handelst du eben dementsprechend.