3

I am looking for the way of disabling proper nodes.

Let's say I have created simple treeview structure like below:

  • Parent 1 (disable)
  • Parent 2
    • Child 1
    • Child 2 (disable)

So far I have found workaround how to do that, but it still doesn't work good at all. I use OnChanging event handler:

procedure TForm1.TreeViewChanging(Sender: TObject; Node: TTreeNode;
  var AllowChange: Boolean); 
begin
   if (Node.Text = 'Parent 1') or (Node.Text = 'Child 1') then
      AllowChange := False;
end;

It works, because I can't click on this node, but the issue is that after 'disabling' node this way, the color of Parent 1 and Child 1 nodes is the same as other. Is it any way to change color of this nodes (grey)? Or maybe you know another way to disabling nodes in TTreeView component?

astack
  • 161
  • 4
  • 16
  • 2
    Set the node's `Enabled` property to `False`. – David Heffernan Sep 22 '16 at 17:34
  • @DavidHeffernan I've tried that, but unfortunately it doesn't work in my case. It does nothing... – astack Sep 22 '16 at 17:38
  • XP perhaps? TVIS_EX_DISABLED is Vista onward. Or perhaps no runtime themes.. – Sertac Akyuz Sep 22 '16 at 17:52
  • I use WIN 8 and I don't know why it doesn't work properly. – astack Sep 22 '16 at 17:56
  • In Win7 I can confirm what @David said, Setting `Enabled` to `False` disables the item but also changes the items color to grey. The difference is not very striking if the font color is the standard `clBlack` but still visible. – Tom Brunberg Sep 22 '16 at 18:17
  • Yes, it's weird, but I still need do find workaround for that... Is it any chance to change node color for this disabled items? – astack Sep 22 '16 at 18:35
  • 1
    @astack - You can draw the items yourself. You should probably find why "enabled" doesn't work though. Did you try with a brand new project? – Sertac Akyuz Sep 22 '16 at 18:46
  • 3
    Find *workaround for what* exactly? The faint difference in shading? Draw the item yourself and paint the disabled items in whatever shade or color you want (Windows uses the default enabled/disabled colors with which the user should already be familiar). Enabled works fine; it prevents selection and alters the shading of the item to show it's disabled. – Ken White Sep 22 '16 at 22:29

1 Answers1

5

To disable a node, set its Enabled property to False. A disabled item is displayed in gray color.

To change the normal color of a nodes text, change TTreeView.Font.Color.

To change the color of a disabled nodes text, use the OnCustomDrawItemevent:

procedure TForm2.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if cdsDisabled in state then
    Sender.Canvas.Font.Color := clRed; //cl3DLight;
end;

Default color settings:

enter image description here

Disabled color = cl3DLight:

enter image description here

Normal text color = clGreen and disabled color = clRed:

enter image description here

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • 2
    Perhaps some commentary on Enabled would be nice. So that future readers can know that it does work. – David Heffernan Sep 22 '16 at 22:35
  • I did it exactly as you and it works for me. First I disabled proper nodes in my treeview and next I used 'OnCustomDrawItem' event to color this nodes on grey color. Thanks! – astack Sep 23 '16 at 15:26