WinForms MultiSelectTreeView-确保不能使用Visible()

时间:2018-07-17 08:51:08

标签: c# winforms treeview

在删除一个TreeNode之后,我希望View在删除的TreeNode之后位于该节点上(以后也应实现相同的功能以编辑节点),但是当前它始终再次显示列表的顶部,我需要手动向下滚动,这对用户可能会很烦。

我使用的是SecureVisible()方法,但不幸的是它不起作用(我正在使用TreeView对其进行测试,该TreeView包含大约30个没有子节点的节点)。

该函数的代码(我认为只有第一行和最后4/5行是相关的):

public override void Remove()
    {
        TreeNode moveToThisNode = treeControl1.SelectedNodes.Last().NextVisibleNode;

        // first, group them by whether they are ObjectGroups or ObjectTypes
        var grouping = from node in treeControl1.SelectedNodes
                       where !node.IsUngroupedNode()
                       let isGroup = node.IsGroupNode()
                       group node by isGroup into g
                       select new { IsGroupNode = g.Key, Items = g };

        foreach (var grp in grouping)
        {
            foreach (var selectedNode in grp.Items)
            {
                // Only allow removal FIRSTLY of ObjectGroups and SECONDLY that are NOT the "ungrouped" group.
                if (grp.IsGroupNode)
                {
                    // Removes the Group
                    var cmd = (Commands.DataCommand<string>)mApplicationData.CommandFactory.Create(string.Concat(CommandPrefix, "Remove"));
                    cmd.Data = selectedNode.Text;
                    cmd.Execute();
                }
                else // ObjectType node --> just move to ungrouped
                {
                    var t = (ObjectType)selectedNode.Tag;
                    var parentNode = selectedNode.Parent;
                    if (parentNode?.IsGroupNode() == true &&
                        parentNode?.IsUngroupedNode() == false) // No removal of ObjectTypes from within "ungrouped"
                    {
                        var group = (ObjectGroup)parentNode.Tag;
                        // Remove the ObjectType from the ObjectGroup but does not delete it -> becomes "ungrouped".
                        var cmd = (Commands.IGroupTypeCommand)mApplicationData.CommandFactory.Create(string.Concat(CommandPrefix, "TypeRemove"));
                        cmd.ObjectClass = t.Class;
                        cmd.ObjectTypeName = t.Name;
                        cmd.Data = group.Name;
                        cmd.Execute();
                    }
                }
            }
        }
        UpdateData();

        if (moveToThisNode!=null)
        {
            moveToThisNode.EnsureVisible();
            MessageBox.Show("Dummy test if moveToThisNode isn't null");
        }
    }

1 个答案:

答案 0 :(得分:0)

我知道了!

问题在于,在Remove()函数之后,调用了我的UpdateData()函数,该函数重绘了所有节点。因此,在此之前调用sureVisible()完全是胡说八道。

所以我要做的是,在Remove()中,将要跳转的节点的名称存储在成员变量中,在UpdateData()的末尾,从TreeNodeCollection中获取具有该名称的节点,然后(最后)为此调用sureVisible()。