1

我得到了以下信息:

Private Sub boldButton_Click(sender As System.Object, e As System.EventArgs) Handles boldButton.Click
    Dim curFont As Font
    Dim newFont As Font
    curFont = rtb.SelectionFont
    If curFont IsNot Nothing Then
        'create the new font
        newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor FontStyle.Bold)
        'set it
        rtb.SelectionFont = newFont
    End If
    updateView()
End Sub

Private Sub italicButton_Click(sender As System.Object, e As System.EventArgs) Handles italicButton.Click
    Dim curFont As Font
    Dim newFont As Font
    curFont = rtb.SelectionFont
    If curFont IsNot Nothing Then
        'create the new font
        newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor FontStyle.Italic)
        'set it
        rtb.SelectionFont = newFont
    End If
    updateView()
End Sub

Private Sub underlineButton_Click(sender As System.Object, e As System.EventArgs) Handles underlineButton.Click
    Dim curFont As Font
    Dim newFont As Font
    curFont = rtb.SelectionFont
    If curFont IsNot Nothing Then
        'create the new font
        newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor FontStyle.Underline)
        'set it
        rtb.SelectionFont = newFont
    End If
    updateView()
End Sub

Private Sub strikethroughButton_Click(sender As System.Object, e As System.EventArgs) Handles strikethroughButton.Click
    Dim curFont As Font
    Dim newFont As Font
    curFont = rtb.SelectionFont
    If curFont IsNot Nothing Then
        'create the new font
        newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor FontStyle.Strikeout)
        'set it
        rtb.SelectionFont = newFont
    End If
    updateView()
End Sub

对我来说似乎有很多重复——除了操作符后面的一小段代码Xor。当事件过程中的代码以这种方式重复时,是否有我应该遵循的标准技术?

只需创建一个方法,每个事件过程使用调用它的按钮的参数以及 'Xor' 后面的变量的 FontStyle 类型的参数来调用它吗?

4

3 回答 3

3

最好的办法是使用一个通用名称的方法,并将所有事件处理程序分配给同一个方法。因此,您希望 sub 的开头行包含;

Handles boldButton.Click, italicButton.Click, underlineButton.Click, strikethroughButton.Click

在此之后,使用“发送者”来确定点击了哪个按钮,从而通过正确的字体样式发送。

做到这一点的最好方法是案例陈述。

以下代码应该适合您,并且已最小化为两种方法。

Private Sub ButtonClickMethod(sender As System.Object, e As System.EventArgs) Handles boldButton.Click, italicButton.Click, underlineButton.Click, strikethroughButton.Click
   Select sender.text
     Case "boldButton"
        MyMethod(FontStyle.Bold)
     Case "italicButton"
        MyMethod(FontStyle.Italic)
     Case "underlineButton"
        MyMethod(FontStyle.Underline)
     Case "strikethroughButton"
        MyMethod(FontStyle.StrikeOut)
    End Select
End Sub

Private Sub MyMethod(style as FontStyle)

   Dim curFont As Font
   Dim newFont As Font
   curFont = rtb.SelectionFont
   If curFont IsNot Nothing Then
       'create the new font
       newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor style)
       'set it
       rtb.SelectionFont = newFont
   End If
   updateView()

End Sub
于 2013-02-28T09:22:39.397 回答
2

Create single Sub using FontStyle as a parameter:

Private Sub UpdateButton(style as FontStyle)
    Dim curFont As Font
    Dim newFont As Font
    curFont = rtb.SelectionFont
    If curFont IsNot Nothing Then
        'create the new font
        newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor style)
        'set it
        rtb.SelectionFont = newFont
    End If
    updateView()
End Sub

And use it in all 3 event handlers:

Private Sub boldButton_Click(sender As System.Object, e As System.EventArgs) Handles boldButton.Click
    UpdateButton(FontStyle.Bold)
End Sub

Private Sub italicButton_Click(sender As System.Object, e As System.EventArgs) Handles italicButton.Click
    UpdateButton(FontStyle.Italic)
End Sub

Private Sub underlineButton_Click(sender As System.Object, e As System.EventArgs) Handles underlineButton.Click
    UpdateButton(FontStyle.Underline)
End Sub
于 2013-02-28T08:52:54.937 回答
2

只是 Jacoooobley 答案的一个侧面选择,从根本上说,他是正确的,只是一种可以使其更具动态性和通用性的方式。

Private Sub ButtonClickMethod(sender As System.Object, e As System.EventArgs) Handles boldButton.Click, italicButton.Click, underlineButton.Click, strikethroughButton.Click
  'This gets us a list of available font styles
  Dim fontStyles = [Enum].GetValues(GetType(FontStyle))
  'This gets a style based on the name (text of a textbox) if it can, if it can't we get `nothing`
  Dim selectedStyle = fontStyles.Cast(Of FontStyle)().FirstOrDefault(Function(x) [Enum].GetName(GetType(FontStyle), x) = sender.Text)
  'Check if this isn't nothing
  If selectedStyle isnot nothing
    'Call `MyMethod`
    MyMethod(selectedStyle)
  end if
End Sub

Private Sub MyMethod(style as FontStyle)

   Dim curFont As Font
   Dim newFont As Font
   curFont = rtb.SelectionFont
   If curFont IsNot Nothing Then
       'create the new font
       newFont = New Font(curFont.FontFamily, curFont.Size, curFont.Style Xor style)
       'set it
       rtb.SelectionFont = newFont
   End If
   updateView()

End Sub

这意味着如果您添加新按钮,则不需要新的案例或方法。

于 2013-02-28T16:16:01.857 回答