将富文本框的 ZoomFactor 增加十进制值
Increasing ZoomFactor of a rich text box by a decimal value
我正在尝试在富文本框(条目)中实现缩放 in/out 功能。我试过将缩放增量增加“1”,但它太大了,使文本在 3-4 次点击后变大。现在我试图将增量值设置为“0.5”以提高缩放精度,但它会发出以下错误:
CS0266 - Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)
我的代码:
private void ts_ZoomIn_Click(object sender, EventArgs e)
{
if (entry.ZoomFactor < 64.5)
{
entry.ZoomFactor = entry.ZoomFactor + 0.5;
}
}
private void ts_ZoomOut_Click(object sender, EventArgs e)
{
if (entry.ZoomFactor > 0.515625)
{
entry.ZoomFactor = entry.ZoomFactor + -0.5;
}
}
我确定有一个简单的修复方法,但过去半小时我一直被这个错误困扰,找不到任何答案。
您收到消息是因为显然 entry.ZoomFactor 是浮点数而 0.5 是双精度数。
警告消息告诉您没有隐式转换,但有显式转换。
这意味着编译器不会为您将 0.5 转换为浮点数 implicity/automatically。但是,您可以像这样将 0.5 转换为浮点数:
entry.ZoomFactor = entry.ZoomFactor + (float) 0.5;
实际上,double和float一起操作时,float会自动转为double,反之则不然。实际上,entry.ZoomFactor + 0.5 的结果是双精度数。因此,以下转换也可能有效:
entry.ZoomFactor = (float)(entry.ZoomFactor+ 0.5);
更好的是,您可以像其中一位评论者所展示的那样避免强制转换,而只需将 0.5 称为 0.5f 使其自然成为单精度浮点数。
entry.ZoomFactor = entry.ZoomFactor + 0.5f;
最后,由于 entry.ZoomFactor 是一个有效的左值,您可以使用 += 运算符。
entry.ZoomFactor += 0.5f;
我正在尝试在富文本框(条目)中实现缩放 in/out 功能。我试过将缩放增量增加“1”,但它太大了,使文本在 3-4 次点击后变大。现在我试图将增量值设置为“0.5”以提高缩放精度,但它会发出以下错误:
CS0266 - Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)
我的代码:
private void ts_ZoomIn_Click(object sender, EventArgs e)
{
if (entry.ZoomFactor < 64.5)
{
entry.ZoomFactor = entry.ZoomFactor + 0.5;
}
}
private void ts_ZoomOut_Click(object sender, EventArgs e)
{
if (entry.ZoomFactor > 0.515625)
{
entry.ZoomFactor = entry.ZoomFactor + -0.5;
}
}
我确定有一个简单的修复方法,但过去半小时我一直被这个错误困扰,找不到任何答案。
您收到消息是因为显然 entry.ZoomFactor 是浮点数而 0.5 是双精度数。
警告消息告诉您没有隐式转换,但有显式转换。
这意味着编译器不会为您将 0.5 转换为浮点数 implicity/automatically。但是,您可以像这样将 0.5 转换为浮点数:
entry.ZoomFactor = entry.ZoomFactor + (float) 0.5;
实际上,double和float一起操作时,float会自动转为double,反之则不然。实际上,entry.ZoomFactor + 0.5 的结果是双精度数。因此,以下转换也可能有效:
entry.ZoomFactor = (float)(entry.ZoomFactor+ 0.5);
更好的是,您可以像其中一位评论者所展示的那样避免强制转换,而只需将 0.5 称为 0.5f 使其自然成为单精度浮点数。
entry.ZoomFactor = entry.ZoomFactor + 0.5f;
最后,由于 entry.ZoomFactor 是一个有效的左值,您可以使用 += 运算符。
entry.ZoomFactor += 0.5f;