我在調整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) - 文本現在是刪除線。
字體發生了什麼變化?文字應加下劃線,斜體不能有刪除線...
這是邏輯錯誤還是我的一個簡單的誤解?
好了,感謝您的時間,我會繼續與它周圍修修補補,直到它的工作或我得到問題的解答,
我想清楚我需要做什麼:檢查我正在調整的特定屬性,並阻止它不止一次添加到字體樣式中。我稍後會發布這個問題的答案。 – Dominick 2011-12-23 06:48:52
登錄已刪除,請參閱FAQ。 – 2011-12-23 16:19:55