开发者

Delete items from a list box with checkboxs enabled. By checking the files you wish to delete

开发者 https://www.devze.com 2023-02-22 03:11 出处:网络
I am trying to delete checked items in my list view my app. The app is simple it deletes temp files inthe current user temp directory. When the app executes it loads all the temp files in the list vie

I am trying to delete checked items in my list view my app. The app is simple it deletes temp files in the current user temp directory. When the app executes it loads all the temp files in the list view. I have check boxes enabled so that the user can check the items that he/she would like to delete Thank you for your time.

Code:

    private void button1_Click(object sender, EventArgs e)
    {
        if (listView1.CheckedItems.Count > 0)
        {
            foreach (var fName in Directory.GetFiles(tFile))
            {
                try
                {
                    File.Delete(fName);
                }
                catch (Exception)
                {
                    // Ignore the failure and continue
                }
            }
            MessageBox.Show("Finished");
            PaintListView(tFile);
        }
        else
        {
            MessageBox.Show("Please Check the files you want to delete"); 
        }
    }开发者_如何学运维




    private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
    {
        foreach (ListViewItem item in listView1.Items)
        {
            item.Checked = true; 
        }
    }

    private void unselectAllToolStripMenuItem_Click(object sender, EventArgs e)
    {
        foreach (ListViewItem item in listView1.Items)
        {
            item.Checked = false;
        }
    }

    private void listView1_ItemChecked(object sender, ItemCheckEventArgs e)
    {
        int c = listView1.CheckedItems.Count;
        for (int i = 0; i < c; i++)
        {
            itemsChecked.Text = i.ToString();
        }

        //int listCount = listView1.CheckedItems.Count;
        //itemsChecked.Text = listCount.ToString(); 
    }


Place the code that you have commented inside a button Click event...for example,

private void btnSubmit_Click(object sender, EventArgs e)
{
       int listCount = listView1.CheckedItems.Count;
       itemsChecked.Text = listCount.ToString(); 
}

And then in this same event handler, inlcude the logic to delete the files that are checked by iterating through the listview contents.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号