2011-12-23 28 views
1

我在調整RichTextBox中的字體樣式時遇到了問題,並且我已經看到了一些討論單個屬性的不同方法(如切換粗體打開和關閉)...但我正在努力使我的字體類可以調整任何屬性(粗體,斜體,下劃線)。在vb.net中調整RichTextBox的字體樣式

我意識到Font.Style是一組布爾標誌(位域?)......但我不知道如何一次處理所有屬性。

這裏是麻煩的代碼:

Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _ 
          Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing) 
    Dim newFontStyle As System.Drawing.FontStyle 


    If Plain Then 
     newFontStyle = Drawing.FontStyle.Regular 
     GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 
     Exit Sub 
    End If 

    If Bold IsNot Nothing Then 
     If Bold Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold 
     End If 
    End If 


    If Italics IsNot Nothing Then 
     If Italics Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic 
     End If 
    End If 

    If Underlined IsNot Nothing Then 
     If Underlined Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline 
     End If 
    End If 

    GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 

End Sub 

這裏是最後的麻煩,此代碼:

  • 我切換粗體(真) - 文雲大膽。
  • 我切換下劃線 - 文本現在是粗體和下劃線。
  • 我切換斜體 - 文本現在是粗體,下劃線和斜體。
  • 我再次切換爲粗體(爲false) - 文本現在是刪除線。

字體發生了什麼變化?文字應加下劃線,斜體不能有刪除線...

這是邏輯錯誤還是我的一個簡單的誤解?

好了,感謝您的時間,我會繼續與它周圍修修補補,直到它的工作或我得到問題的解答,

+0

我想清楚我需要做什麼:檢查我正在調整的特定屬性,並阻止它不止一次添加到字體樣式中。我稍後會發布這個問題的答案。 – Dominick 2011-12-23 06:48:52

+0

登錄已刪除,請參閱FAQ。 – 2011-12-23 16:19:55

回答

1

您使用了錯誤的運營商。它們確實類似於位標誌,枚舉具有[Flags]屬性。您需要使用Or運算符來打開樣式,並使用And運算符關閉樣式。就像這樣:

Dim style = Me.Font.Style 
    '--- turn bold on 
    style = style Or FontStyle.Bold 
    '--- turn bold off 
    style = style And Not FontStyle.Bold 
-1

嗯,我知道了。我已經成功地完成了它。

Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _ 
          Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing) 
    Dim newFontStyle As System.Drawing.FontStyle 


    If Plain Then 
     newFontStyle = Drawing.FontStyle.Regular 
     GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 
     Exit Sub 
    End If 

    If Bold IsNot Nothing Then 
     If Bold And Not GivenFont.Bold Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold 
     End If 
    End If 


    If Italics IsNot Nothing Then 
     If Italics And Not GivenFont.Italic Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic 
     End If 
    End If 

    If Underlined IsNot Nothing Then 
     If Underlined And Not GivenFont.Underline Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline 
     End If 
    End If 

    GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 

End Sub 

感謝您的時間!