Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an error on this line?

Tags:

c#

.net

float startPos = e.Graphics.MeasureString(toMeasure, f);
                    e.Graphics.DrawString(keyword, f, sb, new PointF(e.Bounds.X + (int)startPos, e.Bounds.Y));

This is f:

using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))

And this is toMeasure:

string toMeasure = data[e.Index].Substring(0, keywords - 1);

The error is on the line:

float startPos = e.Graphics.MeasureString(toMeasure, f);

The error is:

Cannot implicitly convert type 'System.Drawing.SizeF' to 'float'

How can I fix it? Since the second line should get float but the first line can't convert from SizeF to float.

like image 835
user2065612 Avatar asked Feb 18 '23 12:02

user2065612


2 Answers

If you want the Width of the string you will have to get the Width from the SizeF stucture returned from MeasureString

Example:

float startPos = e.Graphics.MeasureString(toMeasure, f).Width;
like image 58
sa_ddam213 Avatar answered Feb 20 '23 01:02

sa_ddam213


Method MeasureString returns SizeF object.

SizeF startPos = e.Graphics.MeasureString(toMeasure, f);
like image 22
Kirill Polishchuk Avatar answered Feb 20 '23 00:02

Kirill Polishchuk