我得到了以下内容:
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类型的参数?
答案 0 :(得分:3)
最好的办法是使用一个通用名称的方法,并将所有事件处理程序分配给同一个方法。所以你想要sub的开头行包含;
Handles boldButton.Click, italicButton.Click, underlineButton.Click, strikethroughButton.Click
在此之后,使用“sender”确定单击了哪个按钮,从而通过正确的FontStyle发送。
最好的方法是做个案例陈述。
以下代码应该适合您,并且已经最小化为两种方法。
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
答案 1 :(得分:2)
使用Sub
作为参数创建单个FontStyle
:
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
并在所有3个事件处理程序中使用它:
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
答案 2 :(得分: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
这意味着如果添加新按钮,则不需要新的案例或方法。