更新时间:2023-02-14 07:36:46
listView1.SelectedItems
使用 listView1.CheckedItems
和改变你的 button2_click
来: 私人无效button2_Click(对象发件人,EventArgs E)
{
的foreach(ListViewItem的我在listView1.CheckedItems)
listView1.Items.Remove(I)
}
i currently trying to view all the files and folder selected by the user in a listbox. At the Moment i am able to list what the user have chosen using the openfiledialogue HOWEVER i am now facing prob when i try to remove it form the listbox. i trying to allow the user to click on the checkbox beside the file and press the remove button to remove it
this is my code for remove button
private void button2_Click(object sender, EventArgs e)
{
for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
{
listView1.Items.Remove(listView1.SelectedItems[i]);
}
}
this is the add file to listbox for reference jsut in case
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog = new OpenFileDialog();
// Display open file dialog
openfiledialog.InitialDirectory = "C:\\";
//openfiledialog.Multiselect = true;
openfiledialog.Title = "Lock File";
openfiledialog.Filter = "All Files | *.*";
openfiledialog.ShowDialog();
if (openfiledialog.FileName != "")
{
//move through FileInfo array and store in new array of fi
listView1.Items.Clear();
foreach (string file in openfiledialog.FileNames)
{
listView1.Items.Add(file);
}
}
}
and i pressed the remove button nothing happen and i saw some answer on google on the using of selectionmode but when i used that, my listbox does not have the property of selectionmode and have red lines underlined
Instead of using listView1.SelectedItems
use listView1.CheckedItems
and change your button2_click
to:
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem i in listView1.CheckedItems)
listView1.Items.Remove(i);
}