如何在树视图中突出显示第一个插入的节点? [英] How to highlight the first inserted node in a tree view?

查看:29
本文介绍了如何在树视图中突出显示第一个插入的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份包含两种表格的申请.第一种形式用于以编程方式创建TreeView 节点,第二种形式具有实际的TreeView.当应用程序加载时,我在 TreeView 中创建了两个根节点.

I have an application with two forms. The first form is used to create TreeView nodes programmatically, and the second form has the actual TreeView. When the application loads I create two root nodes in the TreeView.

我的问题是当我为任一根节点创建我的第一个子节点时,它没有突出显示.我给 Form 和 TreeView 焦点,并禁用了 TreeViewHideSelection 属性.

My problem is when I create my first sub-node for either of the root nodes, it is not highlighted. I give the Form and the TreeView focus, and also disabled the HideSelection property for the TreeView.

一旦我将另一个子节点添加到任一根节点,插入的节点就会突出显示.我希望每个插入的节点在插入后都突出显示,但这仅在插入第一个节点后才有效.

Once I add a another sub-node to either of the root nodes is when the inserted node becomes highlighted. I want each inserted node to be highlighted once it has been inserted, but that only works after the first one has been inserted.

示例代码:

m_ObjectAnimationForm.tr_vw_ANIMATION_OBJECT_LIST.SelectedNode = m_ObjectAnimationForm.tr_vw_ANIMATION_OBJECT_LIST.Nodes["OBJECTS_ROOT"].Nodes.Add(NewObject.ID, NewObject.ID);

我使用对象的 ID 和节点的字符串创建一个新的树节点,然后该函数返回新创建的树节点,使其成为树中的选定节点.

I create a new tree node using the ID of the object for the 'KEY' and the string of the node, then that function returns the newly created tree node, making it the selected node in the tree.

在我调用该代码之后:

m_ObjectAnimationForm.tr_vw_ANIMATION_OBJECT_LIST.ExpandAll();
m_ObjectAnimationForm.tr_vw_ANIMATION_OBJECT_LIST.Focus();

推荐答案

我有一个稍微不同的设置,我通过在 datagridview 中选择一个项目来控制 TreeView 中突出显示的项目.这不是最优雅的方法,但确实有效.

I have a slightly different setup, in which I control the highlighted item in the TreeView, by way of selecting an item in a datagridview. It is not the most elegant methodology, but it works.

总结:

1.) 从 Source TreeView、其他控件或其他地方获取索引2.) 展开 Target TreeView 中的所有节点3.) 遍历 Target 中的树节点,直到到达索引4.) 设置 TreeView.SelectedNode = "找到的节点"5.) 在 TreeView 上设置焦点

1.) Get Index from Source TreeView, Other Control, or elsewhere 2.) Expand all Nodes in Target TreeView 3.) Iterate through Tree Nodes in Target, until Index is Reached 4.) Set TreeView.SelectedNode = "the node that was found" 5.) Set Focus on TreeView

    private void selectTreeViewItem(int dataGridViewRowIndex)
    {
        expandAllTreeViewNodes();
        setTreeViewItem(dataGridViewRowIndex);
    }

    private void setTreeViewItem(int dataGridViewRowIndex)
    {
        int iterator = 0;
        TreeNode tempNode = testStepTreeView.Nodes[iterator];
        //don't need to actually return the integer...
        iterator = findNode(tempNode, dataGridViewRowIndex, iterator);
        testStepTreeView.Focus();
        nodeFound = false;
    }

    private void expandAllTreeViewNodes()
    {
        if (testStepTreeView.Nodes.Count != 0)
        {
            foreach (TreeNode x in testStepTreeView.Nodes)
            {
                expandNode(x);
            }
        }
    }

    private void expandNode(TreeNode x)
    {
        if (x.IsExpanded == false)
        {
            x.Expand();
        }
        if (x.Nodes.Count > 0)
        {
            foreach (TreeNode y in x.Nodes)
            {
                expandNode(y);
            }
        }
    }

    private int findNode(TreeNode tempNode, int dataGridViewRowIndex, int iterator)
    {

        if (iterator > dataGridViewRowIndex)
        {
            return iterator;
        }
        if (iterator == dataGridViewRowIndex)
        {
            testStepTreeView.SelectedNode = tempNode;
            nodeFound = true;
            return iterator;
        }

        if (tempNode.Nodes.Count != 0)
        {
            iterator++;
            if (iterator > dataGridViewRowIndex)
            {
                return iterator;
            }
            if (nodeFound == false)
            {
                iterator = findNode(tempNode.Nodes[0], dataGridViewRowIndex, iterator);
            }
        }

        if (tempNode.NextNode != null)
        {
            iterator++;
            if (iterator > dataGridViewRowIndex)
            {
                return iterator;
            }
            if (nodeFound == false)
            {
                iterator = findNode(tempNode.NextNode, dataGridViewRowIndex, iterator);
            }

        }
        return iterator;
    }

这篇关于如何在树视图中突出显示第一个插入的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆