Reputation: 13
I have a listbox whose DataSource is a List who contains folders name. I want delete the folders from listbox and the path, but the first it doesn't work.
Here the code:
private static List<string> themesF;
public Form1()
{
InitializeComponent();
List<string> dirs = new List<string (System.IO.Directory.GetDirectories(@"Thems"));
var pngs = System.IO.Directory.GetFiles(@"Thems").Where(s => s.EndsWith(".png"));
themesF = new List<string>();
for (int i = 0; i < dirs.Count; i++)
{
themesF.Add(dirs[i].Substring(6));
Console.WriteLine("A) Directorio " + dirs[i]);
Console.WriteLine("B) Carpeta" + themesF[i]);
}
lb.DataSource = themesF;
pbx.ImageLocation = (@"Thems\" + themesF[0] + @"\Preview.png");
}
private void btnRemove_Click(object sender, EventArgs e)
{
String folder = lb.SelectedItem.ToString();
themesF.Remove(folder);
lb.DataSource = null;
lb.DataSource = themesF;
System.IO.Directory.Delete(@"Thems\" + folder,true);
}
Upvotes: 1
Views: 409
Reputation: 81645
List<T>
doesn't report changes to the list, so try using a BindingList<T>
instead:
BindingList<string> themesF = new BindingList<string>();
Remove those DataSource lines from the Remove_Click event since those won't be necessary anymore. Just set the DataSource once.
Upvotes: 2
Reputation: 1149
Try this :
private void btnRemove_Click(object sender, EventArgs e)
{
string folder = themesF.Find(t=> t.Equals(lb.SelectedItem.Text));
themesF.Remove(folder);
lb.DataSource = themesF;
System.IO.Directory.Delete(@"Thems\" + folder,true);
}
When you want to delete something from list, one way is find it first. And when you write this : lb.DataSource = something; You don't need to put null there first.
Upvotes: 0