【问题标题】:TreeView AfterExpand event sometimes don't work C#TreeView AfterExpand 事件有时不起作用 C#
【发布时间】:2017-08-04 09:57:03
【问题描述】:

我有一个 TreeView 并订阅了“TreeViewConnections_AfterExpand”和“”事件。 每个人 TreeNode 都包含 MenuScript 事件。和以下代码:

        //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 事件有时不起作用。如图:

但是在这种情况下,我还需要做些什么吗?

【问题讨论】:

  • 是否每次都调用 node.Expand()?
  • 是的。每次调用它,希望它扩展子节点,同时改变节点图标
  • “TreeViewConnections_AfterExpand”事件中的chang节点图标。
  • 第一次展开后是否停止工作?
  • 第一次工作。以后就不行了。不知道为什么?

标签: c# winforms treeview


【解决方案1】:

这个问题是 Node.Collapse 和 Node.Nodes.Clear() 调用引起问题的原因。正确如下:

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();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多