代码之家  ›  专栏  ›  技术社区  ›  whytheq

制作更简洁代码的标准方法,在多个事件过程中重复使用

  •  1
  • whytheq  · 技术社区  · 11 年前

    我收到了以下信息:

    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类型的参数?

    3 回复  |  直到 11 年前
        1
  •  3
  •   Cubsoft    11 年前

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

    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
    
        2
  •  2
  •   LukeHennerley    11 年前

    只是Jacobooobley答案的一个次要选项,从根本上说,他的答案是正确的,只是你可以让它更具动态性和通用性的一种方式。

    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
    

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

        3
  •  2
  •   MarcinJuraszek    11 年前

    创建单个 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