私は次のものを与えられました:
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型のパラメータを作成します。