我得到了以下信息:
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 类型的参数来调用它吗?