Reputation: 475
I am populating a ComboBox
with a list of all the running processes on a system. However, I want to be able to remove all the system processes and general noise from the list.
This is the code I am trying to use:
try
{
var allProc = Process.GetProcesses();
foreach (var p in allProc)
comboBox1.Items.Add(p.ProcessName);
comboBox1.Sorted = true;
comboBox1.Items.Remove("svchost");
}
However I think all it is doing is removing a single instance of the svchost
from the list, when in reality there are many.
How would I remove all of a specific item from the ComboBox
?
Upvotes: 1
Views: 315
Reputation: 9365
First, I don't see why you add those processes name to the combobox, if the next line you remove them. Isn't it simpler to just not add them?
var allProc = Process.GetProcesses().Where(p=>p.ProcessName != "svchost");
foreach (var p in allProc) comboBox1.Items.Add(p.ProcessName);
Second, if you do want to remove item after you've added them, find all the items and remove them with a loop:
foreach (string item in cb.Items.Cast<string>().Where(name => name == "svchost"))
{
cb.Items.Remove(item);
}
Upvotes: 2
Reputation: 157
try this
for (int i = 0; i < comboBox1.Items.Count; i++)
{
string st = comboBox1.Items[i].ToString();
if (st == "svchost")
{
comboBox1.Items.RemoveAt(i);
i--;
}
}
Upvotes: 1