Listview column problems
本文关键字:problems column Listview | 更新日期: 2023-09-27 18:13:53
我有一个列表框和一个列表视图,列表框是用来查看什么程序正在运行,列表视图是过滤一个特定的进程(我在像chrome, mspaint, notepad等列出它),当这些程序正在运行时,列表视图的"状态"列会去"活动",当其中一个程序关闭它去不活动。我的问题是,每当一个程序关闭"状态"列保持"活动",我怎么能改变它为"非活动"时,程序关闭?
private void running_process()
{
Process[] processes = Process.GetProcesses("NAMEOFPC");
// listView1.Items[0].SubItems.Clear();
for (int i = 0; i < listView1.Items.Count; i++)
{
listView1.Items[i].SubItems.Add("Inactive");
foreach (Process p in processes)
{
if (listView1.Items[i].Text == p.ProcessName)
{
listBox1.Items.Add(p.ProcessName);
listView1.Items[i].SubItems[1].Text = "Inactive";
for (int j = 0; j < listBox1.Items.Count; j++)
{
if (listBox1.Items[j].ToString() == listView1.Items[i].Text)
{
// listView1.Items[i].SubItems.Clear();
listView1.Items[i].SubItems[1].Text = "Active";
}
else
{
listView1.Items[i].SubItems[1].Text = "Inactive";
}
}
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
listBox1.Items.Clear();
running_process();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
这样如何:
private void button1_Click(object sender, EventArgs e)
{
var processes = Process.GetProcesses();
foreach (ListViewItem item in this.listView1.Items)
{
item.SubItems[1].Text = ((processes.Any(p => p.ProcessName == item.SubItems[0].Text)
? "ACTIVE"
: "INACTIVE"));
}
}