我有一个处理多个标签的子例程。
Private Sub dgDocuments_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles dgDocuments.DoubleClick
LoadUploadedDocuments()
Try
Dim objTransactionDocument As Objects.TransactionsDocuments
If dgDocuments.CurrentRowIndex.Equals(-1) Then
Dim obj As Object = dgDocuments.Item(dgDocuments.CurrentRowIndex, 1)
objTransactionDocument = Managers.TransactionsDocuments.SelectTransactionsDocuments(Convert.ToInt32(obj))
End If
If Not objTransactionDocument Is Nothing Then
If objTransactionDocument.TransactionsDocumentsID = 1 Then
Dim saveFile As New SaveFileDialog
saveFile.FileName = objTransactionDocument.FileLocation.Substring(objTransactionDocument.FileLocation.LastIndexOf("\"))
saveFile.Title = "Download supporting document to transaction " + m_objTransaction.TransactionID.ToString()
If saveFile.ShowDialog() = DialogResult.OK Then
Dim saveDir As String = Path.GetDirectoryName(saveFile.FileName)
Dim name As String = objTransactionDocument.FileLocation.Substring(objTransactionDocument.FileLocation.LastIndexOf("\"))
Dim saveLocation As String = saveDir + name
System.IO.File.Copy(objTransactionDocument.FileLocation, saveLocation)
MessageBox.Show("Document saved successfully")
End If
End If
End If
Catch ex As Exception
MessageBox.Show("Document saved unsuccessfully " + ex.ToString())
End Try
End Sub
虽然这有效但问题是我希望每个标签每次都有不同的动作,例如
Private Sub Label_click(sender As Object, e As EventArgs) Handles Label1.Click, Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click
checkCards()
End Sub
我希望能够点击每个标签并调用相同的checkCards(labelClicked)
子标签,但是括号的值略有不同。我该怎么做呢?
我对Visual Basic有点新,所以请尽量保持答案基本。
答案 0 :(得分:3)
您的活动的sender
参数是点击的Label
:
Private Sub Label_click(sender As Object, e As EventArgs) Handles Label1.Click, Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click
checkCards(sender)
End Sub
因此,如果您点击Label1
sender
参数包含Label1
对象。
所以你可以像下面那样实现子checkCards
:
Private Sub checkCards(ByVal labelClicked As Label)
Select Case labelClicked.Name
Case Label1.Name
'do some stuff if Label1 was clicked.
Case Label2.Name
'do some stuff if Label2 was clicked.
Case Label3.Name, Label4.Name
'do some stuff if Label3 or Label4 was clicked.
Case Else
'do some stuff if none of the above labels was clicked.
End Select
End Sub