I have the following code to create a Label
on a PictureBox
:
Label l = new Label();
l.Text = _name;
l.Size = CreateGraphics().MeasureString(_name, l.Font).ToSize();
l.BackColor = Color.White;
but the label is always dropping the last character. If I add a character to the call:
l.Size = CreateGraphics().MeasureString(_name+".", l.Font).ToSize();
it works fine, but that doesn't feel right.
There seems to be some white space just before the text in the label, but Padding
is set to 0. How can I fix this the correct way?
Can't you use the
AutoSize
property?MeasureString
is notoriously inaccurate, though normally it returns a size bigger than you'd expect:http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx