How to set ForeColor for a TreeNode?

2019-07-24 13:12发布

问题:

I created a treeview using powershell code but now i want to change the color of the node during the creation depends of the type of the node. I tried this =>

$newNode = new-object System.Windows.Forms.TreeNode  
$newNode.ForeColor = Color.Blue;

But it's not working, i got an error like "The term 'Color.Blue' is not recognized as the name of a cmdlet". Anyone succeed to do it?

回答1:

If you are going to use a typed color and also have intellisense when writing code, you can use:

$newNode.ForeColor = [System.Drawing.Color]::Blue

Also, since the color converter can convert color name and R,G,B value to color, you also can use following options:

$newNode.ForeColor = "Blue"
$newNode.ForeColor = "0,0,255"