Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我在这条线上出错?_C#_.net - Fatal编程技术网

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;