Why am I getting an error on this line?

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 toMeasure

:

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

      

Error in line:

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

      

Mistake:

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

How can I fix this? As the second line needs to be floating, but the first line cannot convert from SizeF

to float

.

+3


source to share


3 answers


If you want Width

for string

, you will need to get Width

from the string SizeF

returned withMeasureString

Example:



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

      

+5


source


The method MeasureString

returns an object SizeF

.



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

      

+2


source


As the error says, the MeasureString function returns System.Drawing.SizeF

which is ordered pair of floating-point numbers

.

0


source







All Articles