Unifosys > Unifosys Support Help Desk > Knowledgebase

Ask a question:

Dynamic Menu - Create Individual Menu for Nodes


Dynamic Menu - Create Individual Menu for Nodes

Question: How can I create a dynamic menu at run-time, for individual nodes of the organization chart?

Answer: You can create menu at run-time, by accessing the ToolStripItemCollection of hpMenu4Node property.

Case 1: Same Menu for All Nodes

You can create a menu anywhere in code, in Form_Load, for example:
IMPORTANT: Use with heNodeClick event to identify the selected node. Alternately, you can attach the node name as menu item tag property, and can access back in menuitem event handler.
private void frmMain_Load( object sender, System.EventArgs e )
    hierarchyWin1.heNodeClick += new HierarchyEventHandler(hierarchyWin1_heNodeClick);
    hierarchyWin1.hpMenu4Node.AvailableOnChart = true;//display
    ToolStripItemCollection items = hierarchyWin1.hpMenu4Node.Items;//get reference
    ToolStripItem tsi = items.Add("Item 1", Properties.Resources.huser, MenuItem_Click);//assign event handler
    tsi.Name = "action1";//name the menu item
string m_sCurName;//hold currently clicked node
void hierarchyWin1_heNodeClick( object sender, HierarchyEventArgs e )
    m_sCurName = e.Node.Name;
void MenuItem_Click(object sender, EventArgs e)
    //Use m_sCurName(object) here together with selected menu (action)
    ToolStripItem tsi = sender as ToolStripItem;
    MessageBox.Show(m_sCurName + ": " + tsi.Text + ": " + tsi.Name);

Case 2: Display Individual (Special) Menus

NOTE: Alternately, you can dynamically create and display node menu in heNodeClick event, depending on the node under consideration:
Snippetprivate void frmMain_Load( object sender, System.EventArgs e )
    hierarchyWin1.heNodeClick += new HierarchyEventHandler(hierarchyWin1_heNodeClick);

Snippetstring m_sCurName;//hold currently clicked node void hierarchyWin1_heNodeClick( object sender, HierarchyEventArgs e ) {     m_sCurName = e.Node.Name;     ContextMenuStrip cms = new ContextMenuStrip();     //if(e.Node.Name == "special")     cms.Items.Add(new ToolStripMenuItem("ItemX", Properties.Resources.huser, MenuItem_Click, "action1"));     cms.Show(PointToClient(MousePosition)); } void MenuItem_Click(object sender, EventArgs e) {     ToolStripItem tsi = sender as ToolStripItem;     MessageBox.Show(m_sCurName + ": " + tsi.Text + ": " + tsi.Name); }

Was this article helpful? yes / no
Article details
Article ID: 22
Category: Support Questions
Date added: 16-Sep-2014
Views: 763
Rating (Votes): Article rated 5.0/5.0 (1)

<< Go back

© 2013

Powered by Help Desk Software HESK™