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.
Well I would do it quite differently, but in keeping with what you have, change these two methods to what I have here (assuming
Listis the name of your ListBox object):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 anObservableCollection<String>). Here is a sample class:In your progHider form you could then have this: