Dynamic MenuItems and Command -Design Question

Topics: CAB & Smart Client Software Factory
Nov 11, 2006 at 2:01 PM
originally posted by: imasud

ToolStripMenuItem = MenuItem in the post below.

Command object works well if you have static menu items.
In my application, I add new Items to menu as the user create some new Connection Objects. For each new object, I create three new sub items, such as delete, select, edit.

MyConnectionObject
|
---------------> Select
|
----------------> Delete
|
-----------------> Edit


I want to implement event handling for these dynamically created menu items. What I had in mind was to create one single method (ConnectionProcessor) whch can handle all connection events. The ConnectionProcessor needs to know the context information. If a user click on Select, I need to know that this select command is for MyConnection object.

In my current solution, I have registered for item (menuitem) events directly.
item.Click += new EventHandler(MyController.ConnectionItem_Processor);

public void ConnectionItem_Processor(object sender, EventArgs e)
{
if (sender is ToolStripItem)
{
ToolStripItem item = sender as ToolStripItem;
ILDAPConnection connection = Connectionsitem.Tag.ToString();
if (connection != null)
{
switch (item.Text)
{
case "Select":
break;
case "Edit":
this.ConnectionEditor.Connection = connection;
ConnectionEditor.Mode = EditMode.Edit;
ConnectionEditor.VendorName = "";
ShowEditor();
break;
case "Delete":
this.Connections.Remove(connection);
break;
default:
break;
}
}
}
}

I want to decouple this direct relation. One way is to Use EventBroker, but I dont know to associate ToolStripMenuItem Events with Event Broker
Can someone suggestion some possible solutions ?
Thnak you so much
Regards
Imran
Nov 11, 2006 at 2:04 PM
originally posted by: imasud

I put this question in the wrong section. Please ignore it. I have move this question to the general section now.
Regards
Imran