Autor Beitrag
ots_sharp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 10.05.12 08:47 
Hallo,

irgendwie habe ich den Faden verloren. Es ist doch möglich die markierten Einträge eine CheckedListBox anzuzeigen ohne die ganzen Items zu durchlaufen und den CheckedStatus abzufragen. Ich schreibe die Info in eine Textdatei, lese diese später ein und setzte die Einstellungen für die CheckedListBox.


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
CheckedListBox objCL = control as CheckedListBox;

for (int z = 0; z < objCL.SelectedItems.Count; z++)
    {
    System.Console.WriteLine(/* Selektierter Name und Index /*)
    }
mats74
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 189
Erhaltene Danke: 26

Win 10
VS 2017/19, C++, C#
BeitragVerfasst: Do 10.05.12 09:20 
Hallo ots_sharp

Ich würde nicht die SelectedItems-Eigenschaft abfragen sondern die CheckedItems-Eigenschaft:
ausblenden C#-Quelltext
1:
2:
3:
4:
for (int z = 0; z < checkedListBox1.CheckedItems.Count; z++)
            {
                // mach mal was
            }

Ich denke schneller kommst Du nicht ans Ziel.

_________________
Gruss
mats74
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 10.05.12 09:31 
Index ist z und Name kommt drauf an was du für Objekt in deiner CheckedListBox verwendest. Ein Item in der SelectedItems oder eher der CheckedItems Liste solltest du also auf den Typ casten den du auch in die CheckedListBox gefüllt hast und an diesem Typ dann eben die Property als Namen ausgeben die du dafür vorgesehen hast. Im einfachsten Fall wenn du strings verwendest hast eben einfach auf string casten oder (nur in diesem Fall) ToString() aufrufen.
ots_sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 10.05.12 09:35 
Danke für die Antwort. Wie komme ich dann an den Index von dem selektierten Item?

Gruß Stefan
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 10.05.12 09:55 
CheckedListBox.SelectedIndex
ots_sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 10.05.12 10:11 
CheckedListBox.SelectedIndex gibt mir nur den ersten Eintrag der markierten Einträge an.

Gruß Stefan
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 10.05.12 10:21 
Dann CheckedListBox.SelectedIndices

Markieren ist zwar ein netter deutscher Begriff aber leider im english bestimmten technischen Kontext ungenau. Meinst du jetzt "selected" oder "checked"?
mats74
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 189
Erhaltene Danke: 26

Win 10
VS 2017/19, C++, C#
BeitragVerfasst: Do 10.05.12 10:40 
Ich würde das, wie Ralf schon erwähnt hat, mit einem Cast auf das jeweilige Item erstellen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
String text = "";
            for (int z = 0; z < checkedListBox1.CheckedItems.Count; z++)
            {
                CheckedListBox clb = checkedListBox1 as CheckedListBox;
                if (clb.SelectedIndex > -1 && clb.SelectedItems.IndexOf(checkedListBox1.CheckedItems[z]) > -1)
                {
                    text += checkedListBox1.CheckedItems[z].ToString() + "/";

                    // oder mach sonst was
                }
                else
                {
                }
            }
            MessageBox.Show(text);

Somit kannst Du prüfen ob der Eintrag gewählt und selektiert ist und danach die Daten weiterverarbeiten.

_________________
Gruss
mats74
ots_sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 10.05.12 14:13 
Sorry, irgendwie bekomme ich immer nur das letzte markierte Element als SelectedItem angezeigt.

Gruß
mats74
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 189
Erhaltene Danke: 26

Win 10
VS 2017/19, C++, C#
BeitragVerfasst: Do 10.05.12 14:29 
Die Eigenschaft SelectionMode bei einer CheckedListBox kannst Du nur auf "None" oder "One" setzen.
Daher wird wahrscheinlich auch nur ein Item selektiert (nicht markiert (in Englisch checked)) und ausgegeben werden.

Wie selektierst Du denn mehrere Items in einer CheckedListBox?

_________________
Gruss
mats74
ots_sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 10.05.12 14:33 
In der CheckListBox sind Items mit Checkboxen. Die können angeklickt/markiert werden.

Gruß Stefan
mats74
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 189
Erhaltene Danke: 26

Win 10
VS 2017/19, C++, C#
BeitragVerfasst: Do 10.05.12 14:42 
... und nicht selektiert (selected und checked ist, wie schon oben erwähnt, nicht ganz das gleiche).
Wenn Du mein Code-Schnippsel oben verstehst, kannst Du einfach den zweiten Teil in der IF-Anweisung weglassen.
Somit erhälst Du alle markierten (checked) items der CheckedListBox.

_________________
Gruss
mats74
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 10.05.12 14:49 
Dann gehe mal davon aus das du mit markieren 'check' meinst. Die relevante Property wäre dann CheckedListBox.CheckedIndices wenn dich nur der Index der gecheckten Einträge interessiert nicht aber die Einträge selbst.
ots_sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 10.05.12 14:56 
Stehe voll auf der Leitung. Sorry, ich verstehs nicht. Im Count sehe ich die 2. Ausgegeben wird immer nur das letzte Item.

Gruß Stefan
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 10.05.12 15:50 
Hallo ots_sharp,

dann schau dir mal das Beispiel zu der CheckedListBox.CheckedIndices-Eigenschaft bzw. der CheckedListBox.CheckedItems-Eigenschaft an!
ots_sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 10.05.12 15:59 
So sollte es jetzt funktionieren:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
foreach (int indexChecked in checkedListBox1.CheckedIndices)
  {
    text +=("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:"     +  checkedListBox1.GetItemCheckState(indexChecked).ToString() + "." + checkedListBox1.Name + "." + checkedListBox1.SelectedItem + "\r\n");

    myFile.Write(indexChecked + ": " + objControl.Name + " - " + objCL.Items[indexChecked] + " - " + objCL.SelectedItems.IndexOf(indexChecked) + "\r\n");
  }
MessageBox.Show(text);


Gruß Stefan
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 10.05.12 16:25 
Vermutlich nicht du verwendest den Index aus CheckedIndices wieder mit SelectedItems. Nochmal selected ist was anders als checked. Vor allem sind die beiden unabhängig von einander.