我有一个列表框,其数据源是包含文件夹名称的列表。我想从列表框和路径中删除文件夹,但第一个不起作用。使用DataSource从列表框中删除项目
下面的代码:
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);
}
使用调试器告诉你为什么。在'String folder = lb.SelectedItem.ToString();'行放置一个停止点并遵循发生的情况。 – LarsTech
文件夹取自lb.selecteditem的值 “文件夹” lb.datasource有17个文件夹s 之前我把value设为null lb.selecteditem现在是“”; themesF有17个文件夹,与数据源完全一样。 随着我删除,我有16个文件夹, lb.Datasource = themesF它有16个文件夹。 所选项目现在是folder1但仍为选定文件夹 – Seeker
使用BindingList而不是List。在click事件中删除这些DataSource行。 – LarsTech