The key is to set the DrawMode of the TreeView to OwnerDraw.Two modes of OwnerDraw exist,OwnerDrawText and OwnerDrawAll.
You need to set the DrawMode of the TreeView to TreeViewDrawMode.OwnerDrawText ,
if of course you dont want to customize the drawing of the nodes on a node-to-node basis.Thats the precise difference between the two,OwnerDrawText customizes the text.
Here is an example that may help you-
public
partial class Form1 : Form
{
Font f = null;
private void InitializeComponent()
{
this.myTreeView = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// myTreeView
//
this.myTreeView.Location = new System.Drawing.Point(37, 15);
this.myTreeView.Name = "myTreeView";
this.myTreeView.Size = new System.Drawing.Size(180, 101);
this.myTreeView.TabIndex = 0;
this.myTreeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(435, 349);
this.Controls.Add(this.userControl11);
this.Controls.Add(this.myTreeView);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
TreeNode clickedNode = myTreeView.GetNodeAt(e.X, e.Y);
if (NodeBounds(clickedNode).Contains(e.X, e.Y))
{
myTreeView.SelectedNode = clickedNode;
}
}
private Rectangle NodeBounds(TreeNode node)
{
// Set the return value to the normal node bounds.
Rectangle bounds = node.Bounds;
if (node.Tag != null)
{
// Retrieve a Graphics object from the TreeView handle
// and use it to calculate the display width of the tag.
Graphics g = myTreeView.CreateGraphics();
int tagWidth = (int)g.MeasureString
(node.Tag.ToString(),f ).Width + 6;
// Adjust the node bounds using the calculated value.
OK - you need to be more specific, as the answer depends on these sort of details. You can do it, you need to write your own class to override the treenodeitem class ( or whatever it's called ) make sure your tree view is using this class instead of the base class, and then still ownerdraw to make your text multi color.
A label won't do this, not in a winforms app ( in a web app you can insert HTML to make it work ). You'd need to write your own control, I don't think there's a multi font or multi color label in the framework. The other alternative is just to draw the text yourself, or use two labels with 2 colors.
no you cant, if you want to you have to override onpaint event handler for the treenode and to draw it yourself you can learn more about drawing in this link
How to change the color of a substring?
Healey6
GDA
Here is an example in CLR 2.0-
The key is to set the DrawMode of the TreeView to OwnerDraw.Two modes of OwnerDraw exist,OwnerDrawText and OwnerDrawAll.
You need to set the DrawMode of the TreeView to TreeViewDrawMode.OwnerDrawText ,
if of course you dont want to customize the drawing of the nodes on a node-to-node basis.Thats the precise difference between the two,OwnerDrawText customizes the text.
Here is an example that may help you-
public
partial class Form1 : Form{
Font f = null;
private void InitializeComponent(){
this.myTreeView = new System.Windows.Forms.TreeView(); this.SuspendLayout(); // // myTreeView // this.myTreeView.Location = new System.Drawing.Point(37, 15); this.myTreeView.Name = "myTreeView"; this.myTreeView.Size = new System.Drawing.Size(180, 101); this.myTreeView.TabIndex = 0; this.myTreeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown); // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(435, 349); this.Controls.Add(this.userControl11); this.Controls.Add(this.myTreeView); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);}
public Form1(){
InitializeComponent();
this.myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawAll;myTreeView.CheckBoxes =
true; this.myTreeView.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode); TreeNode node = new TreeNode("Node"); this.myTreeView.Nodes.Add(node); this.myTreeView.Nodes.Add("SubItem"); this.myTreeView.Nodes.Add("Sub-SubItem");}
void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e){
if (e.Node.Text == "Node"){
Graphics g = e.Graphics;f =
new Font("Arial",12,FontStyle.Italic,GraphicsUnit.Pixel);g.DrawString(
"Node",f ,new SolidBrush(Color.Red),e.Bounds.Left,e.Bounds.Top);}
else{
f = myTreeView.Font;
e.DrawDefault =
true;}
}
private void treeView1_MouseDown(object sender, MouseEventArgs e){
TreeNode clickedNode = myTreeView.GetNodeAt(e.X, e.Y); if (NodeBounds(clickedNode).Contains(e.X, e.Y)){
myTreeView.SelectedNode = clickedNode;
}
}
private Rectangle NodeBounds(TreeNode node){
// Set the return value to the normal node bounds. Rectangle bounds = node.Bounds; if (node.Tag != null){
// Retrieve a Graphics object from the TreeView handle // and use it to calculate the display width of the tag. Graphics g = myTreeView.CreateGraphics(); int tagWidth = (int)g.MeasureString(node.Tag.ToString(),f ).Width + 6;
// Adjust the node bounds using the calculated value.bounds.Offset(tagWidth / 2, 0);
bounds =
Rectangle.Inflate(bounds, tagWidth / 2, 0);g.Dispose();
}
return bounds;}
}
NEC MultiSync90GX2
can't i set i.ForeColor = Color.Red so that the output would be
node no 3
DaveSimmonds
SusanJ
Hi icemart525
you want this to work in Web Application or Desktop Application
Rags
angka
Elaine S
A label won't do this, not in a winforms app ( in a web app you can insert HTML to make it work ). You'd need to write your own control, I don't think there's a multi font or multi color label in the framework. The other alternative is just to draw the text yourself, or use two labels with 2 colors.
MarkZ
hi,
to do that you have override the treenode class but you can colorize the entire string not part of it ine easy steps something like this
for (int i = 0; i < 5; i++){
TreeNode nod = new TreeNode("node no " + i); if (nod.Text == "node no 3")nod.ForeColor =
Color.Red;treeView1.Nodes.Add(nod);
}
hope this helps
Lrose
hi,
no you cant, if you want to you have to override onpaint event handler for the treenode and to draw it yourself you can learn more about drawing in this link
http://www.bobpowell.net/
hope this helps
Sandeep Bhutani