VB.Net 中的布尔表 - Select Case、If-Else 还是其他?

发布于 2024-09-27 08:04:56 字数 2957 浏览 2 评论 0原文

我正在寻求简化三项布尔表达式的处理。 “Select Case”似乎没有提供三重值的解决方案,并且 If 语句似乎有点多。它将按照我编码的方式工作,但如果您对如何简化它有任何想法,我将不胜感激您的见解。如果没有,我希望这个片段可以节省其他人一点时间。

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

I'm looking to simplify the handling of a three-item Boolean expression. "Select Case" doesn't seem offer a solution for triple values, and the If statement seems a bit much. It'll work the way I coded it, but if you have any ideas on how to simplify this, I'd appreciate your insights. If not, I hope this snippet can save someone else a little time.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-10-04 08:04:56

我将使用带有 [Flags] 属性的枚举,然后您可以检查组合。它还将使代码更具可读性。

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

对于 C# 感到抱歉,不知道如何将其转换为 VB.Net

I would use an Enum with the [Flags] attribute and then you can check combinations. It would also make the code more readable.

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

Sorry about the C#, not sure how to translate that to VB.Net

亚希 2024-10-04 08:04:56
 '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
 '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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文