Hi
I'm getting SelectedIndex value -1 after clearing a Listbox and repopulating it. Below is the code:
Code:
- //..........
- public Process[] processes;
- private void Form1_Load(object sender, EventArgs e)
- {
- EnumProcs();
- }
- private void EnumProcs(){
- processes = Process.GetProcesses();
- foreach (Process process in processes)
- {
- //Get whatever attribute for process
- listBox_processes.Items.Add(process.ProcessName.ToString());
- listBox2.Items.Add(process.HandleCount.ToString());
- }
- }
- private void listBox_processes_SelectedIndexChanged(object sender, EventArgs e)
- {
- listBox_processes.Items.Clear();
- processes = null;
- EnumProcs();
- Process ProcessCurrent = processes [listBox_processes.SelectedIndex]; // ** I get -1 as SelectedIndex **
- //.......
Above, I use EnumProcs() to Enumerate system processes and load their names into a Listbox. After 1st population in Form1_Load(), I call EnumProcs() when ever the user selects an item from the Listbox with the proccess names. In SelectedIndexChanged(), I clear() the Listbox before calling EnumProcs(). But when I click an item in the Listbox, SelectedIndex returns -1, though Listbox item count shows 50+.
How to fix this?
Thanks.