I am using Graphics.DrawString to draw my usercontrol's text like this:
protected override void OnPaint(PaintEventArgs e)
{
RectangleF bounds = DisplayRectangle;
bounds.Inflate(-4, -4); // Padding
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Near;
format.Trimming = StringTrimming.None;
using (Brush bFore = new SolidBrush(ForeColor))
{
g.DrawString(Text, Font, bFore, bounds, format);
}
}
If control's Text is wider than the DisplayRectangle, DrawString nicely breaks the Text into multiple lines at word boundaries.
Now I want to underline some words from Text, but I couldn't work it out. I tried splitting the Text, then MeasureString the string just before an underlined part starts, DrawString the normal part, then DrawString the underlined part. But this works only if Text is single-line.
I am sure using a child LinkLabel or RichTextBox to render my control's text will solve this, but I don't like the idea of using a child control just to underline a few words. Is there another way?