Highlighting multiple characters in a text label

I'm searching for a way to highlight multiple characters in a text label using the .NET Compact Framework. E.g. in a label which contains the text Hello World, i want the H and the r highlighted like in this example:

Hello World

My initial solution was to misuse & to underline the target characters, but unfortunately it will only underline one character. I don't care if the characters are in a different color, bold or underlined, the only important thing is that they stand out.




Added color highlighting support.


Tested on .NET Compact Framework 3.5, Windows Mobile 6 SDK, should probably work on the .NET framework too.

/// <summary>
/// A label which offers you the possibility to highlight characters 
/// at defined positions.
/// See <see cref="HighlightPositions"/>, <see cref="HighlightStyle"/> and
/// <see cref="HighlightColor"/>
/// The text in the Text property will be displayed.
/// </summary>
public partial class HighlightLabel : Control
    /// <summary>
    /// Initializes a new instance of the class.
    /// </summary>
    public HighlightLabel()
    /// <summary>
    /// An array of all positions in the text to be highlighted.
    /// </summary>
    public int[] HighlightPositions { get; set; }

    /// <summary>
    /// Gets or sets the highlight style.
    /// </summary>
    public FontStyle HighlightStyle { get; set; }

    /// <summary>
    /// Gets or sets the highlight color.
    /// </summary>
    public Color HighlightColor { get; set; }

    // Paints the string and applies the highlighting style.
    protected override void OnPaint(PaintEventArgs e)
        if (HighlightPositions == null)
            HighlightPositions = new int[] { };

        var usedOffsets = new List<float>();

        for (var i = 0; i < Text.Length; i++)
            var characterToPaint =

            var selectedFont = Font;
            var selectedColor = ForeColor;

            if (HighlightPositions.Contains(i))
                selectedColor = HighlightColor;
                selectedFont = new Font(Font.Name, Font.Size, 

            var currentOffset = usedOffsets.Sum();

            e.Graphics.DrawString(characterToPaint, selectedFont,
                new SolidBrush(selectedColor),
                new RectangleF(e.ClipRectangle.X + currentOffset,
                    e.ClipRectangle.Y, e.ClipRectangle.Width,

            var offset = e.Graphics.MeasureString(characterToPaint,



highlightLabel.HighlightPositions = new[] { 1, 8 };
highlightLabel.HighlightStyle = FontStyle.Bold;
highlightLabel.HighlightColor = Color.Red

Need Your Help

How to make template helper reactive?


I am using a JQueryUI slider in one of my forms to select a value within a range. I want to display this value next to the slider, which means I need to make the getter method for the slider method

HttpClient time-out slow under stress

java http timeout httpclient

I am using HttpClient 3.1 to connect to a different web services. I have set SO_TIMEOUT and ConnectionTimeout to 3 seconds. I am simulating a service that takes more than 3 seconds to respond. Howe...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.