"Gamma" <Ta******@gmail.comwrote:
I'm trying to create a ListBox that holds list of currently running
processes. So I inherited from Process a classes named _Process, which
it's objects are to be held in the ListBox (I just overrode the
"ToString"). Using the "GetProcesses" method, I get Process[], that I
want to cast to _Process[].
You're pretty close - here's the code I got working:
(I'm assuming you have a Form with "ListBox1" on it...)
private void button1_Click(object sender, EventArgs e)
{
// Get the list of processes
Process[] p = Process.GetProcesses();
// Convert that array to our array. We overload ToString() so that the
// ListBox can actually do soemthing meaninfull.
ProcessWrapper []pw = Array.ConvertAll<Process, ProcessWrapper>
(p, new Converter<Process, ProcessWrapper>(ProcessToWrapper));
// Create a new collection of items for the list box.
ListBox.ObjectCollection o = new ListBox.ObjectCollection(listBox1);
// Add in all our processes.
o.AddRange(pw);
}
// This does the conversion
private ProcessWrapper ProcessToWrapper(Process p)
{
return new ProcessWrapper(p);
}
public class ProcessWrapper
{
private Process _process;
public ProcessWrapper(Process p)
{
_process = p;
}
public override string ToString()
{
return _process.ProcessName;
}
}
--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins