C# 为什么我在这条线上出错?
这是C# 为什么我在这条线上出错?,c#,.net,C#,.net,这是f: float startPos = e.Graphics.MeasureString(toMeasure, f); e.Graphics.DrawString(keyword, f, sb, new PointF(e.Bounds.X + (int)startPos, e.Bounds.Y)); 这是测量的: using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Re
f
:
float startPos = e.Graphics.MeasureString(toMeasure, f);
e.Graphics.DrawString(keyword, f, sb, new PointF(e.Bounds.X + (int)startPos, e.Bounds.Y));
这是测量的:
using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
错误在以下行中:
string toMeasure = data[e.Index].Substring(0, keywords - 1);
错误是:
无法将类型“System.Drawing.SizeF”隐式转换为“float”
我怎样才能修好它?因为第二行应该得到float,但是第一行不能从SizeF
转换到float
方法返回SizeF
对象
float startPos = e.Graphics.MeasureString(toMeasure, f);
正如错误所说,函数返回a,这是一对有序的浮点数
如果您想要字符串的宽度
,您必须从度量值
返回的SizeF
结构中获取宽度
例如:
SizeF startPos = e.Graphics.MeasureString(toMeasure, f);
一般来说,当您看到一个方法返回一个类型或希望特定类型作为方法的参数时,您需要为它提供它所期望的…Kirill ok,但现在第二行给出了一个错误,因为它无法将SizeF转换为int:e.Graphics.DrawString(关键字,f,sb,new PointF(e.Bounds.X+(int)startPos,e.Bounds.Y));如果我删除(int)仍然错误,它不能在int和SizeF@user2065612,您只需要从SizeF
中获取宽度
,请参见下面我的答案
float startPos = e.Graphics.MeasureString(toMeasure, f).Width;