[英]Turn private sub into function
我有以下步驟:
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變成一個函數,但是我不確定如何實現。
您可以將處理程序添加到此子。
Private Sub btnRptEmployeePayToMarket_MouseDown(ByValsender As Object, ByVal myButton As System.Windows.Forms.MouseEventArgs) Handles btnRptEmployeePayToMarket.MouseDown, btnAnotherone.MouseDown, etc...
首先,您需要遠離切換標志,以便知道何時切換特定按鈕。
為此,我保留了一個布爾對象字典,以按鈕的名稱為鍵。 當執行通用方法時,如果不存在該標志,它將添加該標志,使用它來確定適當的行為,然后進行切換。
這是使用此邏輯的代碼重寫:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.