Hello guys! This is my current code:

using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace Finalizador_de_Processos
{
    public partial class Form1 : Form
    {
        int IndexProcesso = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ListaProcessos("");
        }
        private void ListaProcessos(string name)
        {
            if (textBox1.Text == "")
            {
                Process[] processos = Process.GetProcesses();
                foreach (Process processo in processos)
                {
                    ListViewItem ProcItem = new ListViewItem(processo.ProcessName, IndexProcesso);
                    ProcItem.SubItems.Add(processo.PagedMemorySize64.ToString());
                    ProcItem.SubItems.Add(processo.Id.ToString());
                    listProcessos.Items.Add(ProcItem);
                    IndexProcesso += 1;
                }
            }
            else
            {
                Process[] processos = Process.GetProcessesByName(name);
                foreach (Process processo in processos)
                {
                    ListViewItem ProcItem = new ListViewItem(processo.ProcessName, IndexProcesso);
                    ProcItem.SubItems.Add(processo.PagedMemorySize64.ToString());
                    ProcItem.SubItems.Add(processo.Id.ToString());
                    listProcessos.Items.Add(ProcItem);
                    IndexProcesso += 1;
                }
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ListaProcessos(textBox1.Text);
        }
    }
}

I'm trying to implement a 'function' that will list the process by name. But, if I just leave the code the way it is, it will append the filtered processes instead of replacing them.

But, if I add an listProcessos.Clear(); before/after this line Process[] processos = Process.GetProcessesByName(name); it don't work at all. What I'm doing wrong?

Thanks in advance!

Nevermind! Solved it using listProcessos.Items.Clear(); instead of listProcessos.Clear(); .

Now works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.