1

我有以下程序:

    Private Sub btnRptEmployeePayToMarket_MouseDown(ByValsender As Object, ByVal myButton As System.Windows.Forms.MouseEventArgs) Handles btnRptEmployeePayToMarket.MouseDown

    Static Toggle As Boolean

    If myButton.Button = MouseButtons.Right Then

        If Toggle Then

            descForm.Hide()

        Else

            descForm.lblReportTitle.Text = "Ranges to Market"
            descForm.txtButtonDescription.Text = "Learn how you are currently paying specific departments or jobs compared to market. "
            descForm.Show()

        End If

    End If
    Toggle = Not Toggle

End Sub

由于我有大约 9 个按钮可以执行相同的操作,但只更改 descForm.lblReportTitle 和 descForm.txtButtonDescription 中的文本,我该如何完成呢?

我想把 sub 变成一个函数,但我不知道如何实现。

4

2 回答 2

1

首先,您需要远离 Toggle 标志,以便知道某个特定按钮何时被切换。

为此,我保留了一个布尔对象字典,以按钮名称为键。当执行通用方法时,如果它不存在,它会添加标志,使用它来确定适当的行为,然后切换它。

这是使用此逻辑对代码的重写:

Private m_cToggleFlags As New System.Collections.Generic.Dictionary(Of String, Boolean)

Private Sub btnRptEmployeePayToMarket_MouseDown(ByVal sender As Object, ByVal myButton As System.Windows.Forms.MouseEventArgs) Handles btnRptEmployeePayToMarket.MouseDown

    ToggleButton(DirectCast(sender, Control).Name, "Ranges to Market", "Learn how you are currently paying specific departments or jobs compared to market.")
End Sub

Private Sub ToggleButton(sButtonName As String, sReportTitle As String, sButtonDescription As String)

    If Not m_cToggleFlags.ContainsKey(sButtonName) Then
        m_cToggleFlags.Add(sButtonName, False)
    End If

    If m_cToggleFlags(sButtonName) 
        descForm.Hide()
    Else
        descForm.lblReportTitle.Text = sReportTitle
        descForm.txtButtonDescription.Text = sButtonDescription
        descForm.Show()
    End If

    m_cToggleFlags(sButtonName) = Not m_cToggleFlags(sButtonName)
End Sub
于 2013-07-27T20:59:21.910 回答
1

您可以向此子添加处理程序。

Private Sub btnRptEmployeePayToMarket_MouseDown(ByValsender As Object, ByVal myButton As System.Windows.Forms.MouseEventArgs) Handles btnRptEmployeePayToMarket.MouseDown, btnAnotherone.MouseDown, etc...
于 2013-07-27T20:54:38.193 回答