J13t0u
J13t0u

Reputation: 841

listbox updating #2

I'm trying to update this Listbox in C#. This is my first program in C#, not exactly sure how things work.

public partial class progHider : Form
{
    String[] processList;

    public progHider()
    {
        InitializeComponent();
    }

    private void progHider_Load(object sender, EventArgs e)
    {
        List.Items.AddRange(getList());
    }

    private String[] getList()
    {
        Process[] processlist = Process.GetProcesses();
        processList = new String[Process.GetProcesses().Length];
        int index = 0;
        foreach (Process process in processlist)
        {
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
            {
                processList[index] = process.MainWindowTitle;
                index++;
            }
        }

        return processList;
    }

    private void btnrefresh_Click(object sender, EventArgs e)
    {
        List.DataSource = null;
        this.Update();
    }

So the refresh button is suppose to update the Listbox by calling getList(), but I'm not sure how to get it done. In java, you just need to call the method and do repaint(). I tried this.refresh/update, no use. One question is how do I update Listbox? I can't figure out how to accomplish it.

Am I even doing this right? Should List.Items.AddRange(getList()); be in the progHider_Load method? Another question is, how does private void progHider_Load(object sender, EventArgs e) work? Is it only used once? can you call it? Also, where is the Main method? I'm using Visual Studio 2010 windows application mode, it just shows me the code for the partial class.

Upvotes: 2

Views: 526

Answers (2)

Jason Down
Jason Down

Reputation: 22191

Well I would do it quite differently, but in keeping with what you have, change these two methods to what I have here (assuming List is the name of your ListBox object):

private void progHider_Load(object sender, EventArgs e)
{
    List.DataSource = getList();
}

private void btnrefresh_Click(object sender, EventArgs e)
{
    List.DataSource = getList();
}

And to answer you question. The progHider_Load event gets called when the form first loads. You shouldn't be calling it explicitly (though there is nothing stopping you if you really wanted to).


UPDATE:

I would probably pull your process related code out of the form and put it in another class. Then create a property with a BindingList<String> (for WinForms... or if you are using WPF you would likely use an ObservableCollection<String>). Here is a sample class:

using System;
using System.ComponentModel;
using System.Diagnostics;

public class ProcessListGenerator
{
    public ProcessListGenerator()
    {
        ProcessList = new BindingList<String>();
    }

    public BindingList<String> ProcessList
    {
        get;
        private set;
    }

    public void UpdateProcessList()
    {
        ProcessList.Clear();
        foreach (var proc in Process.GetProcesses()
                                    .Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)))
        {
            ProcessList.Add(proc.MainWindowTitle);
        }
    }

In your progHider form you could then have this:

public partial class progHider : Form
{
    ProcessListGenerator _processes;

    public progHider()
    {
        InitializeComponent();
    }

    private void progHider_Load(object sender, EventArgs e)
    {
        _processes = new ProcessListGenerator();
        _processes.UpdateProcessList();
        listBox1.DataSource = _processes.ProcessList;
    }

    private void btnRefresh_Click(object sender, EventArgs e)
    {
        _processes.UpdateProcessList();
    }
}

Upvotes: 5

Matthew Sanford
Matthew Sanford

Reputation: 1099

It looks on your button click even you need to call List.Items.AddRange(getList());

for the purposes of your example you can call progHider_Load.. but in my opnion calling event handlers is dirty pool

Upvotes: 0

Related Questions