我有一個列表框,其數據源是包含文件夾名稱的列表。我想從列表框和路徑中刪除文件夾,但第一個不起作用。使用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