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

最善の方法は、共通名の単一のメソッドを使用し、すべてのイベントハンドラーを同じメソッドに割り当てることです。したがって、サブのオープニングラインに含める必要があります。

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

この後、「送信者」を使用して、クリックされたボタンを判別し、正しいFontStyleを介して送信します。

これを行う最良の方法は、caseステートメントです。

次のコードはあなたのために働くはずです、そしてそれは2つの方法に最小化されました。

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

パラメータとして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
于 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 に答える