【发布时间】:2018-07-21 08:06:54
【问题描述】:
在 Visual Studio 2017 C# RichTextBox 中比较字体时,为什么完整字体比较失败? - 我已经包含了失败的 [if 语句] 代码,然后是传递的 [if 语句] 代码。我猜这是 UI 或编译器中的 [Strongly Typed] 问题,但我想澄清一下。谢谢
RichTextBox testMemo = new RichTextBox();
testMemo.Font = new Font("Calibri", 12);
Font testFont = new Font("Calibri", 12);
//这些都失败了
if (memo1.Font != testMemo.Font)
{
memo1.Font = new Font("Calibri", 12);
}
else
{
memo1.Font = new Font("Courier", 12);
}
if (memo1.Font != testFont)
{
memo1.Font = new Font("Calibri", 12);
}
else
{
memo1.Font = new Font("Courier", 12);
}
if (memo1.Font.FontFamily != testMemo.Font.FontFamily)
{
memo1.Font = new Font("Calibri", 12);
}
else
{
memo1.Font = new Font("Courier", 12);
}
if (memo1.Font.FontFamily != testFont.FontFamily)
{
memo1.Font = new Font("Calibri", 12);
}
else
{
memo1.Font = new Font("Courier", 12);
}
// 这些工作正常
if (memo1.Font.Name != testMemo.Font.Name && memo1.Font.SizeInPoints != testMemo.Font.SizeInPoints)
{
memo1.Font = new Font("Calibri", 12);
}
else
{
memo1.Font = new Font("Courier", 12);
}
if (memo1.Font.Name != testFont.Name && memo1.Font.SizeInPoints != testFont.SizeInPoints)
{
memo1.Font = new Font("Calibri", 12);
}
else
{
memo1.Font = new Font("Courier", 12);
}
【问题讨论】:
-
编写一个扩展方法并与字体系列和字体大小进行比较。
-
好建议,虽然我觉得应该内置用于字体比较
-
因为
Font是一个引用类型。因此,您需要按照建议比较所有相关属性.. -
您确实注意到 FontFamily 比较失败的地方,对吧?
标签: c# fonts richtextbox