I have Process objects that are monitored from two different views. A Windows.Forms.ListView (actually a derived class) and a Graph Viewer (based on Microsoft Research's Automatic Graphing Layout). Each has a context menu which can have similar events activated. While the list view can have multiple selections, I don't allow it on the graph view.
This is what I currently have:
private void ctxgrphAddBreakpoint_Click(object sender, EventArgs e)
{
Process p = GetProcess(viewer);
if (p != null)
{
p.AddBreakpoint();
BeginRefresh(false, false);
}
}
private void ctxgrphRemoveBreakpoint_Click(object sender, EventArgs e)
{
Process p = GetProcess(viewer);
if (p != null)
{
p.RemoveBreakpoint();
BeginRefresh(false, false);
}
}
private void ctxlistAddBreakpoint_Click(object sender, EventArgs e)
{
foreach (Process p in lvwProcessList.SelectedProcesses())
{
p.AddBreakpoint();
}
BeginRefresh(false, false);
}
private void ctxlistRemoveBreakpoint_Click(object sender, EventArgs e)
{
foreach (Process p in lvwProcessList.SelectedProcesses())
{
p.RemoveBreakpoint();
}
BeginRefresh(false, false);
}
I'd like to unify the two context menus into one and the event handling into one like this:
private void ctxlistAction_Click(object sender, EventArgs e)
{
// I can unify the Viewer and ListView and implement some common interface,
// so I'm pretty sure I can handle this part
foreach (Process p in UIView.SelectedProcesses())
{
p.Action(); // What is the best way to handle this?
}
BeginRefresh(false, false);
}
How do I get there?