I try to change font size in runtime as following code, but I always get StackOverflowException.
protected override void OnResize(EventArgs e)
{
this.bitmapDoubleBuffer = new Bitmap(base.Width, base.Height);
this.backGraphics = Graphics.FromImage(this.bitmapDoubleBuffer);
}
public void ReDrawRuntime()
{
SizeF sizeF = this.backGraphics.MeasureString(this.Text, this.Font);
if (sizeF.Width > this.Size.Width)
{
this.Font = new Font(this.Font.Name, 20, this.Font.Style);
sizeF = this.backGraphics.MeasureString(this.Text, this.Font);
}
this.backGraphics.DrawString(this.Text,
this.Font,
this.solidBrushForeColor,
(float)((int)width),
(float)((int)height));
this.graphicsDoubleBuffer.DrawImage(this.bitmapDoubleBuffer, 0, 0);
}
Cause changing font size wil increase the size of the control the text drwn on, which will riase Resize event agin (look on
this.Font =.., this !!) , which will run your code again, which probabbly (according to the exception) will validate to true:condition again and so on…
So general rule is: do not change shape of the control inside
OnResize, do it in another place.