TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Kasun Lee
NA
178
14.1k
Problem after listbox clear()
Nov 14 2017 3:19 AM
Hi
I'm getting SelectedIndex value -1 after clearing a Listbox and repopulating it. Below is the 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.
Reply
Answers (
3
)
DivideByZeroException exception
Find two .xml file path from one a text file