Autor Beitrag
O5IRI5
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 36
Erhaltene Danke: 2

XP/Vista
C# (VS 2005) , eclipse
BeitragVerfasst: Sa 17.11.07 17:28 
Habe für ein Listview Objekt ein Kontextmenü angelegt.
Ich möchte per Kontextmenü alle markierten Einträge löschen.
Mein Problem:

Wenn ich nun die zu löschenden Einträge markiere und die Kontextmenüfunktion ausführe, werden die Einträge gelöscht.
Befinde ich mich beim Aufrufen (Rechtsklick) des Kontextmenüs nicht direkt über den markierten Elementen verlieren die Einträge die Markierung und somit wird kein Eintrag gelöscht.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
private void clearToolStripMenuItem1_Click(object sender, EventArgs e)
        {         
                while(this.lvPlaylist.SelectedItems.Count>0)
                {
                    this.lvPlaylist.Items.Remove(this.lvPlaylist.SelectedItems[0]);
                }           
        }



LG
Harry


Moderiert von user profile iconChristian S.: Topic aus Allgemeine .NET-Fragen verschoben am Sa 17.11.2007 um 17:43
Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 17.11.07 19:12 
Folgende Lösung finde ich selber nicht elegant, aber sie funktioniert ;)
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
        private int[] selIndices;

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && listView1.GetItemAt(e.X, e.Y) == null)
            {
                selIndices = new int[listView1.SelectedIndices.Count];
                listView1.SelectedIndices.CopyTo(selIndices, 0);
            }        
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && listView1.GetItemAt(e.X, e.Y) == null)
            {
                foreach (int idx in selIndices)
                    listView1.SelectedIndices.Add(idx);
            }
        }

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
O5IRI5 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 36
Erhaltene Danke: 2

XP/Vista
C# (VS 2005) , eclipse
BeitragVerfasst: So 18.11.07 15:56 
Danke!