【问题标题】:RichTextBox Comparing Fonts FailRichTextBox 比较字体失败
【发布时间】: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


【解决方案1】:

Font.Equals 方法呢? 而不是

memo1.Font != testMemo.Font

你会的

Dim fnttestMemo As Font = testMemo.Font
Dim fntMemo1 As Font = memo1.Font
If fnttestMemo.Equals(fntMemo1) = False Then
...
End If

https://docs.microsoft.com/fr-fr/dotnet/api/system.drawing.font.equals?view=dotnet-plat-ext-6.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2016-07-05
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多