我有一些行,我希望用户能够通过 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();
猛跑小猪
杨__羊羊
相关分类