Autor Beitrag
Csharp-programmierer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 696
Erhaltene Danke: 10

Windows 8.1
C# (VS 2013)
BeitragVerfasst: Do 17.09.15 19:22 
Moin Leute. Auf meiner Form befindet sich eine CheckedListBox, die 5 Items beinhaltet. Außerdem gibt es auf der Form einen Button. Drück man diesen Button, soll überprüft werden, welche Items gecheckt wurden. Wählt man z.B. Item3, soll z.B. eine Variable deklariert werden. Wenn man hingegen Item1 auswählt, soll der Text der Form gleich "Hallo" sein. Wisst ihr was ich meine? Prüfen welche Items gecheckt wurden.

Mfg :)
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 17.09.15 19:27 
CheckedListBox hat eine CheckedItems sowie eine CheckedIndices Property, je nachdem was dir besser passt.

Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Csharp-programmierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 696
Erhaltene Danke: 10

Windows 8.1
C# (VS 2013)
BeitragVerfasst: Sa 19.09.15 10:32 
Ich habe es jetzt so probiert, was jedoch nicht hinhaut:
ausblenden C#-Quelltext
1:
2:
3:
4:
if(this.checkedListBox1.SelectedIndices[5] == true)
{
    MessageBox.Show("Eigenschaft 5");
}

Können Sie mir vielleicht weiterhelfen?

Moderiert von user profile iconTh69: Beitragsformatierung überarbeitet.
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Sa 19.09.15 10:41 
Nur wenn du das Siezen einstellst.

Für diesen Beitrag haben gedankt: Csharp-programmierer
Csharp-programmierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 696
Erhaltene Danke: 10

Windows 8.1
C# (VS 2013)
BeitragVerfasst: Sa 19.09.15 10:51 
Ich habe jetzt einen Code entwickelt, der nur zum Teil hinhaut:
ausblenden C#-Quelltext
1:
2:
3:
4:
if(this.checkedListBox1.SelectedItem.ToString() == "<center>")
{
    MessageBox.Show("<center>");
}

Wenn ich jetzt nur "<center>" auswähle, wird der Code ausgeführt, jedoch möchte ich ja auch mehreres checken können. Dann würde dieser Algorithmus nicht mehr funktionieren.

Moderiert von user profile iconTh69: Beitragsformatierung überarbeitet.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4805
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Sa 19.09.15 11:03 
ausblenden C#-Quelltext
1:
checkedListBox1.CheckedItems.Any(x => x.ToString() == "<center>");					


Zuletzt bearbeitet von Th69 am Sa 19.09.15 11:04, insgesamt 1-mal bearbeitet
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Sa 19.09.15 11:03 
Selected ist was anderes als Checked. Das mußt du auseinander halten. Dich interessiert der checked Zustand eines Eintrags nicht ob der selektiert ist.
Wir sind also wieder bei den bereits angesprochenen CheckedItems. Wenn ich dich richtig verstehe möchtest du mehrere Einträge checken können dann durch irgendeine Aktion ausgelöst für alle gecheckten Einträge irgendwas ausführen das könnte man z.B. so lösen.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
foreach (string item in checkedListBox1.CheckedItems)  // string geht hier nur wenn man auch tatsächlich strings an die ListBox gebunden hat!
{
    switch (item)
    {
        case "<center>":
            MessageBox.Show("<center>");
            break;
        case "anderer Text":
            // mach hier was anderes 
            break;
        // etc.
    }
}
Csharp-programmierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 696
Erhaltene Danke: 10

Windows 8.1
C# (VS 2013)
BeitragVerfasst: Sa 19.09.15 20:15 
Vielen Dank. Klaüüt jetzt alles :)