2010-10-12 46 views
1

我正在尋找簡化三項布爾表達式的處理。 「Select Case」似乎沒有提供三重值的解決方案,而If語句似乎有點多。它會以我編碼的方式工作,但如果您對如何簡化這一點有任何想法,我會很感激您的見解。如果沒有,我希望這個片段可以節省一些時間。VB.Net中的布爾表 - Select Case,If-Else或Something else?

Public Sub SetThisFontStyle(ByRef theTextBox As RichTextBox, ByVal StyleToChange As String) 

    'Get Initial Values:' 
    Dim BoldState As Boolean = theTextBox.SelectionFont.Bold 
    Dim ItalicState As Boolean = theTextBox.SelectionFont.Italic 
    Dim UnderlineState As Boolean = theTextBox.SelectionFont.Underline 

    'Find out what we re trying to change:' 
    Select Case StyleToChange 
     Case "Bold" : If BoldState Then BoldState = False Else BoldState = True 
     Case "Italic" : If ItalicState Then ItalicState = False Else ItalicState = True 
     Case "Underline" : If UnderlineState Then UnderlineState = False Else UnderlineState = True 
     Case Else : Exit Sub 
    End Select 

    'Boolean Table just for reference:' 
    '0 - 0 - 0 None' 
    '1 - 0 - 0 Bold Only' 
    '0 - 1 - 0 Italic Only' 
    '0 - 0 - 1 Underline Only' 
    '1 - 1 - 0 Bold and Italic' 
    '0 - 1 - 1 Italic and Underline ' 
    '1 - 0 - 1 Bold and Underline' 
    '1 - 1 - 1 Bold, Italic, and Underline' 

    If Not BoldState And Not ItalicState And Not UnderlineState Then 
     'Regular, without any styles' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Regular) 
    ElseIf BoldState And Not ItalicState And Not UnderlineState Then 
     'Bold Only' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold) 
    ElseIf Not BoldState And ItalicState And UnderlineState Then 
     'Italic Only' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Italic) 
    ElseIf Not BoldState And Not ItalicState And UnderlineState Then 
     'Underline Only' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Underline) 
    ElseIf BoldState And ItalicState And Not UnderlineState Then 
     'Bold and Italic' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold + Drawing.FontStyle.Italic) 
    ElseIf Not BoldState And ItalicState And UnderlineState Then 
     'Italic and Underline' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Italic + Drawing.FontStyle.Underline) 
    ElseIf BoldState And Not ItalicState And UnderlineState Then 
     'Bold and Underline' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold + Drawing.FontStyle.Underline) 
    ElseIf BoldState And ItalicState And UnderlineState Then 
     'Bold, Italic, and Underline' 
     theTextBox.SelectionFont = _ 
     New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold + Drawing.FontStyle.Italic + Drawing.FontStyle.Underline) 
    Else 
     Exit Sub 
    End If 

End Sub 
+0

你可以嘗試治療三種結果就像一個三位數字和使用switch語句(可能看起來更醜並且比你已經擁有的更少)。 – 2010-10-12 20:51:58

回答

1

我會使用Enum和[Flags]屬性,然後你可以檢查組合。它還會使代碼更具可讀性。

[Flags] 
public enum TextSettingType 
{ 
    None = 0x0, 
    Bold = 0x1, 
    Italic = 0x2, 
    Underline = 0x3 
} 

很抱歉的C#,不知道如何翻譯,爲VB.Net

1
'Boolean Table just for reference:' 
     '0 - 0 - 0 None' = 0 
     '1 - 0 - 0 Bold Only' = 4 
     '0 - 1 - 0 Italic Only' = 2 
     '0 - 0 - 1 Underline Only' = 1 
     '1 - 1 - 0 Bold and Italic' = 6 
     '0 - 1 - 1 Italic and Underline' = 3 
     '1 - 0 - 1 Bold and Underline' = 5 
     '1 - 1 - 1 Bold, Italic, and Underline' = 7 

     Dim bold As Boolean = False 
     Dim italic As Boolean = False 
     Dim under As Boolean = False 
     Dim fNum As Byte 

     If chkBold.Checked Then 
      bold = True 
     End If 
     If chkItalic.Checked Then 
      italic = True 
     End If 
     If chkUnder.Checked = True Then 
      under = True 
     End If 

     fNum = ((4 * Convert.ToByte(bold)) + (2 * Convert.ToByte(italic)) + (1 * Convert.ToByte(under))) 
     'MsgBox(fNum) 

     Select Case fNum 
      Case 0 
       ' Regular 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Regular) 
      Case 1 
       ' Underline 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Underline) 
      Case 2 
       ' Italic 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Italic) 
      Case 3 
       ' Italic and Underline 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Italic Or FontStyle.Underline) 
      Case 4 
       ' Bold 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Bold) 
      Case 5 
       ' Bold and Underline 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Bold Or FontStyle.Underline) 
      Case 6 
       ' Bold and Italic 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Bold Or FontStyle.Italic) 
      Case 7 
       'Bold, Italic, and Underline 
       txtEntry.Font = New Font("Arial", 12, FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline) 
     End Select