0
votes

I'm trying to add a handler to MouseLeftButtonUp of my dynamically created Label (System.Windows.Controls.Label) but I'm getting an error message

Argument not specified for parameter 'e' of 'Private Sub btnLink_Clicked(sender As Object, e As MouseButtonEventArgs)

Private Sub CreateClickableLabel()

   Dim btnLink As New System.Windows.Controls.Label()

   btnLink.Content = "Click Me"
   AddHandler btnLink.MouseLeftButtonUp, btnLink_Clicked

   Windows.Controls.Grid.SetColumn(btnLink, 0)
   Windows.Controls.Grid.SetRow(btnLink, 0)
   gridData.Children.Add(btnLink)
End Sub


Private Sub btnLink_Clicked(ByVal sender As Object, ByVal e As MouseButtonEventArgs)

   MessageBox.Show("You clicked me")

End Sub

How can I add this event Handler programmatically to my Labels? once I'll have more labels being handled by the same function?

1

1 Answers

1
votes

You are missing the AddressOf operator:

AddHandler btnLink.MouseLeftButtonUp, AddressOf btnLink_Clicked

In VB.NET you've got to use AddressOf every time you refer to a method (sub or function) without calling/executing it. For instance if you want to pass it as a parameter to another method/statement/operator, or if you want to store it in a variable and use it in your code.

This is needed because AddressOf creates a Delegate of your method. A Delegate is basically an object-oriented wrapper around the method's pointer, meaning you can now treat it like any other object in .NET.

Read more: Delegates (Visual Basic) | Microsoft Docs