Graphics to TextRenderer

N

Nathan Laff

I have a custom label, that as the width changes, the height will change to
fit all the text in the label...

I use to do this like this...

int newHeight = (int)Math.Ceiling(g.MeasureString(this.Text,
this.Font, this.Width - this.Padding.Left,
StringFormat.GenericDefault).Height);

Then override the onPaint and do this...


e.Graphics.DrawString(this.Text, this.Font, new
SolidBrush(this.ForeColor), new
Rectangle(this.Padding.Left,this.Padding.Top,this.Width -
this.Padding.Left,this.Height), StringFormat.GenericDefault);

Works great.

Now I'm trying to move to TextRenderer and can't seem to get it to do either
of these things.

I'm trying to measure like this...

int newHeight = TextRenderer.MeasureText(g, this.Text,
this.Font, this.Size, TextFormatFlags.Bottom).Height;

and draw like this...

TextRenderer.DrawText(e.Graphics, this.Text, this.Font,
this.ClientRectangle, this.ForeColor, this.BackColor,
TextFormatFlags.Default);


The label doesn't resize, and even if I use the resize code from the
Graphics.MeasureString, the TextRender.DrawText doesn't work.

Please help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top