实际上取消选择 TreeView (.NET) 中的所有节点 [英] Actually deselect all nodes in TreeView (.NET)

查看:41
本文介绍了实际上取消选择 TreeView (.NET) 中的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实际上取消选择我的 TreeView 中的所有节点.实际上",我的意思是 TreeView1.SelectedNode = null; 实际上会取消选择树中的节点.

I'm trying to actually deselect all nodes in my TreeView. By "actually", I mean that TreeView1.SelectedNode = null; will actually deselect the node in the tree.

现在,从视觉上来说,节点被取消选择.但是,当我尝试添加新节点时,树视图将自动选择树中的第一个节点(在顶部)并在理想情况下我想创建父节点时创建一个子节点.我不能在添加之前取消选择选定的节点,因为用户可能想要添加一个子节点.我想要的行为是基于树视图中选择的内容添加父/子节点.如果什么都没有选择,则添加一个父节点,如果选择了某个节点,则在该选定节点中添加一个子节点.

Right now, visually speaking, the node is deselected. However, when I try to add a new node, the treeview will automatically select the first node in the tree (at the top) and create a subnode when ideally I want to create a parent node. I can't just deselect the selected node before adding either, because the user might want to add a child node. The behaviour I'd like is for the parent/child node adding to be based on what is selected in the treeview. If nothing is selected, add a parent, if something is selected, add a child in that selected node.

我在一个带有图像和文本的函数中构造了一个名为 nodeTreeNode 对象,然后我有以下内容:

I construct a TreeNode object called node in a function with images and text and all that then I have the following:

        if (tvContent.SelectedNode == null)
            tvContent.Nodes.Add(node);
        else
        {
            tvContent.SelectedNode.Nodes.Add(node);
            tvContent.SelectedNode.Expand();
        }

我有一个取消全选"按钮,可以让上面的代码工作.该按钮的代码很简单:

I have a "Deselect All" button that is supposed to make the above code work. The code for that button is simply:

tvContent.SelectedNode = null;

请原谅我同时标记 C# 和 VB.NET.我对这两种语言都很好,所以如果有人能用任何一种语言帮助我,那就太棒了!

Pardon my tagging both C# and VB.NET. I'm good with both so if someone can help me out in either language that'd be terrific!

谢谢

有趣的.似乎在测试所选节点是否为空时,.NET 会自动将所选节点设置为树中的第一个节点.以下代码显示了触发器"消息框,但在 if 语句完成后立即选择树中的第一个节点.

Interesting. It seems that while testing if the selected node is null, .NET will automatically set the selected node to the first node in the tree. The following code shows the "trigger" message box, but immediately selects the first node in the tree after the if statement is complete.

    private void btnDeselectAll_Click(object sender, EventArgs e)
    {
        tvContent.SelectedNode = null;
        if (tvContent.SelectedNode == null) MessageBox.Show("trigger");
    }

问题在于使用 InputBox 作为节点的标题输入.无论出于何种原因,这都会更改树视图的选定节点.我在一个股票项目中尝试了这个并设法复制了这个问题.我想没有解决这个问题:S

The issue lies in using an InputBox for the title input of the node. For whatever reason, that changes the selected node of the treeview. I tried this in a stock project and managed to replicate the issue. I guess there's no fixing this :S

推荐答案

所以事实证明,获得真正的"取消选择是不可能的.一旦树视图失去焦点然后再次获得焦点(例如通过弹出的输入框窗口),所选节点将不再为空.

So it turns out that getting "true" deselection isn't possible. As soon as the treeview loses focus then gains focus again (e.g. via an inputbox window popping up), the selected node will no longer be null.

我的工作是引入一个面板,该面板通过一些输入选项变得可见,以便节点标题输入在主窗体上而不是在另一个窗体上完成.我不喜欢这个修复,但它是所有可以做的.

My work around was to introduce a panel that becomes visible with some input options so that node title input is done on the main form instead of on another form. I don't like this fix but it's all that can be done.

这篇关于实际上取消选择 TreeView (.NET) 中的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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