TreeNode ForeColor 更改 SelectedNode.ForeColor

发布于 2024-10-06 19:05:04 字数 1627 浏览 2 评论 0原文

我在 TreeNode 对象上设置了前景色。稍后,当我单击此节点时,直到释放鼠标后 SelectedNode.ForeColor 才会更改。

TreeNode.ForeColor = Color.Red;

所有具有 ForeColor == Color.Empty 的 TreeNode 在鼠标单击时立即获得正确的 SelectedNode.ForeColor。

如果我使用键盘移动选择,它会按预期工作。但不是鼠标点击。如何将节点的 ForeColor 设置为 Color.Black 并在第一次单击鼠标时获得正确的 SelectedNode.ForeColor?

我像这样填充TreeView。在所有附加了“颜色”的 TreeNode 对象上,我在按下鼠标时得到前景色(红色)..直到我释放鼠标左键并获得正确的白色前景色..

private void Form1_Load(object sender, EventArgs e)
{
  for (int i = 0; i < 3; i++)
  {
    TreeNode node = new TreeNode("node_" + i);

    for (int j = 0; j < 3; j++)
    {
      TreeNode childNode = new TreeNode("childNode_" + i + j);

      if (j==0)
      {
        childNode.Text += "color";
        childNode.ForeColor = Color.Red;
      }

      node.Nodes.Add(childNode);
    }
    treeView1.Nodes.Add(node);
  }
}

如果我这样做..

treeView1.FullRowSelect = false; //if this is true it don't work.. hm!
treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);

void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
  e.DrawDefault = true;
}

它有效!嗯。这是 .NET TreeView 默认行为中的错误吗?

当然......

//treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
//treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);

现在它恢复正常了..并且不起作用!谁能解释一下为什么!! 在这里,当我单击节点上的“左侧”按钮时,我会得到蓝色背景和节点前景色。在它上面等待,直到我释放鼠标按钮,然后正确绘制选择背景和前景色(在我看来)。

笔记!如果 FullRowSelect 为 true。上面的 DrawNode 代码不起作用。它将在除节点部分之外的所有行上绘制蓝色背景颜色(它被绘制为白色)。

I set the ForeColor on a TreeNode object. And later when I click this nodes the SelectedNode.ForeColor isn't changed until after I release the mouse.

TreeNode.ForeColor = Color.Red;

All TreeNodes with ForeColor == Color.Empty get the proper SelectedNode.ForeColor immediately on mouse click.

If I move selection using the keyboard it work as expected. But not on mouse click. How do I set the ForeColor of nodes to e.g. Color.Black and get the correct SelectedNode.ForeColor on the first mouse click?

I fill the TreeView like this. On all TreeNode object with "color" appended I get the ForeColor (Red) on mouse down.. until I release the left mouse button and get the proper white ForeColor..

private void Form1_Load(object sender, EventArgs e)
{
  for (int i = 0; i < 3; i++)
  {
    TreeNode node = new TreeNode("node_" + i);

    for (int j = 0; j < 3; j++)
    {
      TreeNode childNode = new TreeNode("childNode_" + i + j);

      if (j==0)
      {
        childNode.Text += "color";
        childNode.ForeColor = Color.Red;
      }

      node.Nodes.Add(childNode);
    }
    treeView1.Nodes.Add(node);
  }
}

If I do this..

treeView1.FullRowSelect = false; //if this is true it don't work.. hm!
treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);

void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
  e.DrawDefault = true;
}

It works! Hm. Is this a bug in the default behavior of .NET TreeView?

And of course..

//treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
//treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);

.. now it's back to normal.. And don't work!! Can anyone explain why!?!
Here I get the blue background and Node ForeColor when I click the Left button on a node. Above it waits until I release the mouse button and then paint the selection background- and fore color correct (in my opinion).

Note! If FullRowSelect is true. The DrawNode code above don't work. It will paint the blue background color on all the row except for the node part (it's painted white).

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

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

发布评论

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

评论(3

别再吹冷风 2024-10-13 19:05:05

您是否尝试过 NodeMouseClick 或 < href="http://msdn.microsoft.com/en-gb/library/system.windows.forms.treeview.beforeselect.aspx" rel="nofollow">BeforeSelect 事件?

希望这有帮助,

Have you tried with NodeMouseClick or BeforeSelect events ?

Hope this helps,

马蹄踏│碎落叶 2024-10-13 19:05:05

将树视图的 HideSelection 属性设置为 false。

make HideSelection property of treeview as false.

卸妝后依然美 2024-10-13 19:05:04

设置鼠标点击事件在鼠标按下时触发。

set the mouse click event to trigger on mouse down.

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