I have this method to open and fill checkedlistbox.
void Show_files()
{
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog() == DialogResult.OK)
{
checkedListBox1.Items.Clear();
string[] files = Directory.GetFiles(FBD.SelectedPath);
string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
foreach (string file in files)
{
checkedListBox1.Items.Add(file);
}
foreach (string dir in dirs)
{
checkedListBox1.Items.Add(dir);
}
}
}
and then button to delete selected files.
private void button1_Click(object sender, EventArgs e)
{
foreach (var item in checkedListBox1.CheckedItems.OfType<string>().ToList())
{
DialogResult dialogResult = MessageBox.Show("Czy na pewno chcesz usunać zaznaczone pliki", "Czy na pewno chcesz usunać zaznaczone pliki?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
File.Delete(item);
}
}
MessageBox.Show("Usunięto");
checkedListBox1.Refresh(); // here should refresh list of folder content
}
I've tried to clear and add items again in loop (if items count not null) in method Show_files() but it hasn't worked. Hide and show neither. I don't want to open folder browser again. It has to happen automatically.
You aren't removing the item from the control:
Best to put some Try...Catch blocks around such operations.