How to change the color of a substring?

for example:

string newStr = "The color is red";

label1.Text = newStr;

output:

The color is red




Answer this question

How to change the color of a substring?

  • BadChoice

    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.

  • Suroor Fatima - MSFT

    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



  • jenright

    can't i set i.ForeColor = Color.Red so that the output would be

    node no 3



  • vbnm

    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.



  • Gnome

    looks like a lot of work, thanks! U

  • Tomas Hellström

    it's a win app, i'm trying to use it at the treeview control, i want to display the e.node.text with different colors for some reasons.

  • oddessa

    yes that helps, thanks!

  • E Renken

    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



  • Emu

    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;

    }

    }


  • Paul Rayner

    Hi icemart525

    you want this to work in Web Application or Desktop Application

    Rags



  • How to change the color of a substring?