1
votes

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).

3

3 Answers

0
votes

set the mouse click event to trigger on mouse down.

0
votes

Have you tried with NodeMouseClick or BeforeSelect events ?

Hope this helps,

0
votes

make HideSelection property of treeview as false.