I'm trying to add a handler, but as soon as I'm targetting a method that has parameters, the handler fails. This is the simple code:
AddHandler App.Current.RootVisual.MouseLeftButtonUp, RootVisual_MouseLeftButtonUp
Private Sub RootVisual_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
End Sub
This error won't let me build. When looking at the examples, I'm doing it right. This is the error I get:
Error 3 Argument not specified for parameter 'e' of 'Private Sub
RootVisual_MouseLeftButtonUp(sender As Object, e As
System.Windows.Input.MouseButtonEventArgs)'. C:\TFS\ProjectCollection\ItemManagementTool\ItemManagementTool.ClientApplication\Views\MainMenu.xaml.vb 82 70 ItemManagementTool.ClientApplication
I get a similar error for the "sender" parameter.
Any ideas?
You are missing the AddressOf keyword
AddHandler App.Current.RootVisual.MouseLeftButtonUp, AddressOf RootVisual_MouseLeftButtonUp
Related
In our VB.NET code, we are using many event handlers.
For below line of code we are getting the syntax error "BC30469 Reference to a non-shared member requires an object reference" where we use an optional parameter in the function signature:
AddHandler TabControl.Click, AddressOf FrmMW.ProcessTabClickEvent
where the function signature is
Public Sub ProcessTabClickEvent(sender As System.Object, e As System.EventArgs, Optional OpenNewTab As Boolean = False)
But for the below case, i.e. without optional parameters, we are not getting the syntax error:
AddHandler menuItem.Click, AddressOf SelectAllAction
where the function signature is
Private Sub SelectAllAction(ByVal sender As Object, ByVal e As EventArgs)
Please assist in solution. Our code is getting built successfully in VB Express 2010, but we are getting this error in VB Express 2017.
I got a problem when using this add handler. It keeps on saying
"AddressOf operand must be the name of a method (without parentheses)".
I am not sure what I have done wrong for this to occur.
The code for the add handler:
AddHandler NudQuantityOfItem(a).SelectedItem, AddressOf TotalPrice(T)
The code for the Totalprice:
`Public Sub Totalprice(ByVal a As Integer)
For T = 1 To CInt(CustomerMenu.NudQuantityOfItem.SelectedItem)
TotalItemPriceCalculation(T, a)
Next
End Sub`
The code for the Quantity:
NudQuantityOfItem(a) = New numericupdowncounter
NudQuantityOfItem(a).Location = New Point(X, Y)
NudQuantityofitem(a).Width = 23 : cboQuantity(i).Height = 33`
I'm not positive of this but I think the target of teh AddressOf needs the same signature as hooked event.
e.g.
AddHandler c.TextChanged, AddressOf SetDirty
Private Sub SetDirty(ByVal sender As Object, ByVal e As System.EventArgs)
If Not mbDirty AndAlso Not mbFormLoad Then
mbDirty = True
....
End Sub
Note that SetDirty as the same parameters as does TextChanged.
In your case you would use Sender to defined the control that changed and then find the required value.
I have 3 check boxes that I want to act as a radial button group, but my new job is in vb.net and WinForms.
My hope was to simplify the 3 event handlers by replacing the commented code with a function call like this:
Private Sub cbLinqQuery_CheckedChanged(sender As Object, e As EventArgs) handles cbLinqQuery.CheckedChanged
SafeCheckChg(cbDataTable, cbDataTable_CheckedChanged, cbLinqQuery.Checked)
'RemoveHandler cbDataTable.CheckedChanged, AddressOf cbDataTable_CheckedChanged
'cbDataTable.Checked = Not cbLinqQuery.Checked
'AddHandler cbDataTable.CheckedChanged, AddressOf cbDataTable_CheckedChanged
RemoveHandler cbArray.CheckedChanged, AddressOf cbArray_CheckedChanged
cbArray.Checked = Not cbLinqQuery.Checked
AddHandler cbArray.CheckedChanged, AddressOf cbArray_CheckedChanged
btnDefaultBoundData_Click(sender, e)
End Sub
Private Sub SafeCheckChg(ByRef cb As CheckBox, ByRef handler As EventHandler, checked As Boolean)
RemoveHandler cb.CheckedChanged, AddressOf handler
cb.Checked = Not checked
AddHandler cb.CheckedChanged, AddressOf handler.Clone
End Sub
But I've had no success passing the event handler as a parameter. I've tried passing it as an event handler, a delegate and a void but using it in the RemoveHandler/AddressOf call doesn't seem to be understood by the compiler.
Is there a method where I can get the event handler off of the passed object? Is there a type I can use as the parameter. Obviously I'm not desperate, this is in the name of better code not better functionality.
So Found a pretty close answer that worked with a small tweak:
Inspiration
Solution was simply to pass AddressOf foo as the parameter and remove the AddressOf from the RemoveHandler / AddHandler call. (Which for some reason I thought was a required part of the Add/RemoveHandler call and not a separate function call operating on a parameter.)
Final Code:
Private Sub cbLinqQuery_CheckedChanged(sender As Object, e As EventArgs) Handles cbLinqQuery.CheckedChanged
SafeCheckChg(cbDataTable, AddressOf cbDataTable_CheckedChanged, cbLinqQuery.Checked)
SafeCheckChg(cbArray, AddressOf cbArray_CheckedChanged, cbLinqQuery.Checked)
btnDefaultBoundData_Click(sender, e)
End Sub
Private Sub SafeCheckChg(ByRef cb As CheckBox, ByRef handler As EventHandler, checked As Boolean)
RemoveHandler cb.CheckedChanged, handler
cb.Checked = checked
AddHandler cb.CheckedChanged, AddressOf handler.Clone
End Sub
I am working on Infopath and VBA and facing a trivial issue. I have tried searching on this and found a few examples but could not comprehend them correctly (being a novice).
I will really appreciate if any one can push me in the right direction.
I am trying to accomplish executing code for four buttons (button1,2,3,4) by clicking a button called MasterSumbit
I have the event handlers loaded in the InternalStartup section
Private Sub InternalStartup(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Startup
AddHandler DirectCast(EventManager.ControlEvents("Button1"), ButtonEvent).Clicked, AddressOf Button1_Clicked
AddHandler DirectCast(EventManager.ControlEvents("Button2"), ButtonEvent).Clicked, AddressOf Button2_Clicked
...and such for button 3 and 4
AddHandler DirectCast(EventManager.ControlEvents("MasterSubmit"), ButtonEvent).Clicked, AddressOf MasterSubmit_Clicked
End Sub
This is the code for the click event on the MasterSubmit Button
Public Sub MasterSubmit_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
Button1_Clicked.click()
Button2_Clicked.click()
Button3_Clicked.click()
Button4_Clicked.click()
End Sub
I get the following errors for each button
Argument not specified for parameter 'e' of 'Public Sub Button1_Clicked(sender As Object, e As Microsoft.Office.InfoPath.ClickedEventArgs)'.
Argument not specified for parameter 'sender' of 'Public Sub Button1_Clicked(sender As Object, e As Microsoft.Office.InfoPath.ClickedEventArgs)'.
Thanks in anticipation
Try this:
Public Sub MasterSubmit_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
Button1_Clicked.click(sender, e)
Button2_Clicked.click(sender, e)
Button3_Clicked.click(sender, e)
Button4_Clicked.click(sender, e)
End Sub
Would (inside the Sub for Master button) this not work?
Button2.PerformClick()
Button3.PerformClick() etc....
Hope I helped.
it seems that adding for example a button Dim myButton as New Button and then addHandler to mySub("lol", 255) is not possible.
Where mySub is Shared Sub MySub(byRef myString as string, myInteger as Integer)
So: addHandler myButton.click, addressOf mySub("lol", 255) - returns an error saying it does not work with parentheses or whatever.
I somehow see why this might not be possible, so I'm looking for a work-around on this problem.
Please help _jakeCake
First of all the syntax for AddHandler would be:
AddHandler myButton.click, AddressOf mySub
Secondly the signature of the eventhandler procedure must match the signature of the event like so:
Private Sub myButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
[...]
End Sub
Maybe you could look into using a lambda expression when you add the event. When using lambda's in VB.NET the function must return a value and does not support multi-line statements.
Dim myButton As New Button
AddHandler myButton.Click, Function(senderObj, args) myFunc("lol", 255)