2017-08-04 1 views
3

Ich habe eine TreeView und abonniert die "TreeViewConnections_AfterExpand" und "" -Ereignis. Jeder TreeNode enthält MenuScript-Ereignisse. und der folgende Code:TreeView AfterExpand Event funktioniert manchmal nicht C#

 //event 
private void TreeViewConnections_AfterExpand(object sender, TreeViewEventArgs e) 
    { 
     var activeKey = e.Node.ImageKey.Replace("Inactive", "Active"); 
     e.Node.ImageKey = activeKey; 
     e.Node.SelectedImageKey = activeKey; 
    } 

//event 
private void TreeViewConnections_MouseClick(object sender, MouseEventArgs e) 
    { 
     var currentNode = this.treeViewConnections.GetNodeAt(e.Location); 
     if (currentNode == null) return; 
     var currentBounds = currentNode.Bounds; 
     Rectangle bounds = new Rectangle(currentBounds.Left - ExpandIcon.Width, currentBounds.Y, currentBounds.Width - 5, currentBounds.Height); 
     if (bounds.Contains(e.Location)) return; 
     this.treeViewConnections.SelectedNode = currentNode; 

     if (e.Button == MouseButtons.Right) 
     { 
      SetupConnectionMenus(currentNode); 
     } 
    } 

    private void SetupConnectionMenus(TreeNode node) 
    { 
     var isOpened = node.Nodes.Count > 0; 
     if (node.ContextMenu == null) 
     { 
      var menu = new ContextMenuStrip(); 
      menu.Items.AddEx("Open Connection", node.Name + "_Open", !isOpened, onClick: OpenConnection_Click, context: node); 
      menu.Items.AddEx("Close Connection", node.Name + "_Close", isOpened, onClick: CloseConnection_Click, context: node); 
      node.ContextMenuStrip = menu; 
     } 
    } 

    //event 
    private void OpenConnection_Click(object sender, EventArgs e) 
    { 
     var menuItem = sender as ToolStripMenuItem; 
     var currentNode = menuItem.Tag as TreeNode; 
     OpenConnection(currentNode); 
    } 

    //event 
    private void CloseConnection_Click(object sender, EventArgs e) 
    { 
     var menuItem = sender as ToolStripMenuItem; 
     var currentNode = menuItem.Tag as TreeNode; 
     currentNode.Nodes.Clear(); 
     currentNode.Collapse(); 
    } 

private void OpenConnection(TreeNode node) 
    { 
     treeViewConnections.BeginUpdate(); 
     //add child node to the node. 
     treeViewConnections.EndUpdate(); 
     node.Expand(); //????? 
    } 

TreeViewConnections_AfterExpand Ereignis irgendwann funktioniert nicht. wie in der Abbildung dargestellt:

enter image description here

Aber in diesem Fall ist es etwas anderes brauche ich?

+0

Wird node.Expand() jedes Mal aufgerufen, wenn Sie erwarten? – Reniuz

+0

ja. Jedes Mal, wenn ich es anrufe, hoffe ich, dass es untergeordnete Knoten erweitert und gleichzeitig das Knotensymbol –

+0

chang Knoten-Symbol im Ereignis "TreeViewConnections_AfterExpand" ändert. –

Antwort

1

Dieses Problem ist die Ursache der Node.Collapse und Node.Nodes.Clear() - Aufruf hat Probleme verursacht. die richtige wie folgt:

private void CloseConnection_Click(object sender, EventArgs e) 
    { 
     var menuItem = sender as ToolStripMenuItem; 
     var currentNode = menuItem.Tag as TreeNode; 
     currentNode.Collapse(); // Here will verify whether the current node has child nodes. 
     currentNode.Nodes.Clear(); 
    } 
Verwandte Themen