Autor Beitrag
Yankyy02
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 138
Erhaltene Danke: 21

Win 11 x64
C# (VS 2022 - Rider)
BeitragVerfasst: Mo 10.09.12 20:33 
Servus miteinander,

ich hätte eine Frage wie der Titel schon sagt wie man einen Eintrag aus einer ListBox löschen sollte!?

Ich habe hierzu 3 Beispiele und es wäre nett wenn mir jemand erklären könnte wie man es richtig macht bzw.
welche die beste Lösung ist damit ich für mich überprüfen kann ob ich es auch wirklich richtig nachvollzogen
habe!

Nr. 1

ausblenden C#-Quelltext
1:
2:
ListBox.ObjectCollection items = this.myListBox.Items;
items.Remove(items[myListBox.SelectedIndex]);


Nr. 2

ausblenden C#-Quelltext
1:
2:
Object item = mylistBox.Items[myListBox.SelectedIndex];
myListBox.Items.Remove(item);


Nr. 3

Und das ist mein persönlicher Favorit
(obwohl da sicher einer denken wird --> Ah Hauptsache es funktioniert)

ausblenden C#-Quelltext
1:
myListBox.Items.Remove(myListBox.SelectedItem);					


Ich wäre sehr dankbar wenn mir jemand da kurz helfen könnte!


MfG

Gery
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Mo 10.09.12 21:22 
Also, mein Favorit wäre auch Nr.3. Aber ich binde meine ListBox immer an eine List<ListBoxItem> oder an eine ObserveableCollection<Object> und lösche dann aus der Auflistung, und zwar wie gehabt mit Nr.3. :-)

_________________
Gruß
Christoph

Für diesen Beitrag haben gedankt: Yankyy02
Yankyy02 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 138
Erhaltene Danke: 21

Win 11 x64
C# (VS 2022 - Rider)
BeitragVerfasst: Di 11.09.12 10:56 
Danke für deine Antwort!

Aber sieht jemand Vorteile gegenüber den anderen bzw. Nachteile gegenüber meiner
"Favorit" Variante ?!

MfG
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Di 11.09.12 16:28 
Nr.3 ist ein Einzeiler :-)

_________________
Gruß
Christoph
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 11.09.12 18:05 
Hi,

ich würde ja zu Nr. 4 tendieren:
ausblenden C#-Quelltext
1:
myListBox.Items.RemoveAt(myListBox.SelectedIndex);					

Bei den anderen Varianten könnte es sein, falls ein Objekt mehrfach in der Liste vorhanden ist, daß das erste in der Liste entfernt wird (und nicht unbedingt das mittels SelectedIndex ausgewählte) - ist natürlich nur als Spezialfall zu sehen (z.B. bei Wertetypen wie Zahlen).

Aber bei einer datengebundenen ListBox sollte man selbstverständlich den Eintrag aus der Liste entfernen (wie Christoph1972 schon geschrieben hat).

Für diesen Beitrag haben gedankt: Yankyy02
Yankyy02 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 138
Erhaltene Danke: 21

Win 11 x64
C# (VS 2022 - Rider)
BeitragVerfasst: Mi 12.09.12 10:05 
Servus,

super danke für deine Antwort!

MfG