Instead of listBox1_SelectedIndexChanged, update the list by clicking the mouse button, otherwise your code will look normal. Initially, you probably do not have an item in your list, and that why SelectedIndexChanged does not start when you click on it.
Edit: (Since the question has been edited, I will update my answer)
To unzip your lists using files, you have to do this, in some cases, except SelectedIndexChanged. Since your lists are not filled at the beginning of your application, and the SelectedIndexChanged event is fired when there are elements in the list, and the user clicks on it. You can create the following function
private void PopulateListBox(ListBox lsb, string Folder, string FileType) { DirectoryInfo dinfo = new DirectoryInfo(Folder); FileInfo[] Files = dinfo.GetFiles(FileType); foreach (FileInfo file in Files) { lsb.Items.Add(file.Name); } }
Now you can call this function with your list in any event by clicking a button or loading a form. eg.
private void Form1_Load(object sender, EventArgs e) { PopulateListBox(listbox1, @"C:\TestLoadFiles", "*.rtld"); PopulateListBox(listbox2, @"C:\TestLoadFiles", "*.other"); }
Habib source share