C# 停止选择一个或多个 TreeNode 的 Treeview

发布于 2024-07-11 07:40:48 字数 454 浏览 12 评论 0原文

我有一个 TreeView 控件,显示有组织的层次结构中的多个 TreeNode。 我想阻止用户选择最高级别的节点(这是通过使用 BeforeSelect 事件实现的)。 如果用户选择顶级节点,我还想阻止 TreeView 突出显示顶级节点,即阻止 TreeView 更改节点的背景颜色并“选择”它。

我使用的 TreeView 是 WinForms 版本的控件。

下面是我当前尝试使用的源代码:

private void tree_BeforeSelect ( object sender, TreeViewCancelEventArgs e )
{
    if ( e.Node.Level == 0 )
    {
        e.Cancel = true;
    }
}

这确实会取消选择节点,但只有在明显的闪烁(~200ms)之后才会取消选择,这是不希望的。

I have a TreeView control showing multiple TreeNodes in an organised heirarchy. I want to stop the user selecting the highest level Nodes (this was achieved by using the BeforeSelect Event). I also want to stop the TreeView from highlighting the top level nodes if the user selects them i.e. stop the TreeView from changing the background color of the node and 'selecting' it.

The TreeView that I am using is the WinForms version of the control.

Below is the source code I am currently attempting to use:

private void tree_BeforeSelect ( object sender, TreeViewCancelEventArgs e )
{
    if ( e.Node.Level == 0 )
    {
        e.Cancel = true;
    }
}

This does de-select the Node but only after a noticible flash (~200ms) which is undesirable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2024-07-18 07:40:48

此代码可防止在取消选择之前绘制选择:

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
    treeView1.BeginUpdate();
}

private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
    treeView1.EndUpdate();
}

This code prevents drawing the selection before it is canceled:

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
    treeView1.BeginUpdate();
}

private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
    treeView1.EndUpdate();
}
执笔绘流年 2024-07-18 07:40:48

除了现有代码之外,如果您使用代码向 TreeView 上的 MouseDown 事件添加处理程序并使用其位置选择节点,则可以设置节点颜色。

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
    TreeNode tn = treeView1.GetNodeAt(e.Location);
    tn.BackColor = System.Drawing.Color.White;
    tn.ForeColor = System.Drawing.Color.Black;
}

仍然存在一个小问题,即选择轮廓仍然显示在 MouseDown 上,但它至少会停止蓝色背景并让您走得更远。

HTH

OneSHOOT

In addition to your existing code if you add a handler to the MouseDown event on the TreeView with the code and select the node out using it's location, you can then set the nodes colours.

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
    TreeNode tn = treeView1.GetNodeAt(e.Location);
    tn.BackColor = System.Drawing.Color.White;
    tn.ForeColor = System.Drawing.Color.Black;
}

There is still a slight problem in that the select outline still shows on MouseDown but it atleast stops the blue background and gets you a little further.

HTH

OneSHOT

十二 2024-07-18 07:40:48

如果通过在 BeforeSelect 的事件参数中将 Cancel 设置为 true 来取消选择,则不会选择该节点,因此背景颜色不会更改。

If the selecting is cancelled by setting Cancel to true in the BeforeSelect's event args, the node will not be selected and thus the background color won't change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文