选择然后删除选定的列表项

我有一些行,我希望用户能够通过 ctrl+a 选择多个行,然后可以通过按键盘上的 Delete 键删除所选项目。


我的代码不起作用。可以通过 ctrl+A 选择列表项,但按删除按钮后,只会删除一项。


private void lvTasks_KeyDown(object sender, KeyEventArgs e)

{

     if (e.KeyCode == Keys.A && e.Control)

    {

        lvTasks.MultiSelect = true;

        foreach (ListViewItem item in lvTasks.Items)

        {

            item.Selected = true;

        }

    }

    if (e.KeyCode == Keys.Delete) 

    foreach (ListViewItem eachItem in lvTasks.SelectedItems)

    {

         DeleteTask();

    }

}




public void DeleteTask()

{

    if (lvTasks.SelectedItems.Count > 0)

    {



            lvTasks.Items.Remove(lvTasks.SelectedItems[0]);

            Statics.SaveTasks(lvTasks.Items.Cast<ListViewItem>().Select(l => (Task)l.Tag).ToArray());

            UpdateTaskProgress(new Task());

            txtDownloadLink.Text = "";

            txtRarPassword.Text = "";

            txtReferenceLink.Text = "";

            chkBackupResource.Checked = Properties.Settings.Default.BackupSource;

            chkClearExtracted.Checked = Properties.Settings.Default.CleanExtract;

            chkRemoveRar.Checked = Properties.Settings.Default.RemoveRar;

            chkUseIDM.Checked = Properties.Settings.Default.UseIDM;

            chkRemoveDownload.Checked = Properties.Settings.Default.RemoveDownload;

            gbTaskProgress.SuspendLayout();

            settings = Statics.LoadSettings();

            gbTaskProgress.Controls.Clear();

            int max = Enum.GetValues(typeof(TaskProgresses)).Cast<int>().LastOrDefault();

            for (int i = 0; i <= max; i++)

                gbTaskProgress.Controls.Add(new TaskProgress(i + 1) { Left = 10, Top = (20 + 24 * i), Caption = ((TaskProgresses)i).ToString().CapitalSplit(), Task = new Task() });

            gbTaskProgress.ResumeLayout();

            lvTasks.Items.Clear();



繁星点点滴滴
浏览 159回答 2
2回答

猛跑小猪

private void lvTasks_KeyDown(object sender, KeyEventArgs e){&nbsp; &nbsp; if (e.KeyCode == Keys.A && e.Control)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;lvTasks.MultiSelect = true;&nbsp; &nbsp; &nbsp;foreach (ListViewItem item in lvTasks.Items)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;item.Selected = true;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; if (e.KeyCode == Keys.Delete)&nbsp;&nbsp; &nbsp; for (int i = lvTasks.SelectedItems.Count - 1; i >= 0; i--)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;ListViewItem itm = lvTasks.SelectedItems[i];&nbsp; &nbsp; &nbsp;lvTasks.Items[itm.Index].Remove();&nbsp; &nbsp; }}编辑你的线路 lvTasks.Items.Remove(lvTasks.SelectedItems[0]);这是问题所在。您只是删除索引 0 处的项目。您需要对它们进行循环。编辑 2仅当您要删除所有项目时才有效。如果您真的只想删除选定的项目。更改 for 循环并删除 List :for (int i = lvTasks.SelectedItems.Count - 1; i >= 0; i--){&nbsp;ListViewItem itm = lvTasks.SelectedItems[i];&nbsp;lvTasks.Items[itm.Index].Remove();}那应该有效。

杨__羊羊

我会使用一个while循环:if (e.KeyCode == Keys.Delete)&nbsp;{&nbsp; &nbsp;while (lvTasks.SelectedItems.Count > 0)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; lvTasks.SelectedItems[0].Remove();&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP