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.
Related
I have a function GetLogs that calls another function GetHistoricalData. GetHistoricalData is running in a thread and at the end of it, it fires an event AsyncLoggingRequestFinished. What I want is to wait in GetLogs for the event to be raised. Here is a code example to make thinks clearer:
Private Sub GetLogs
_historicalLogging = new HistoricalLogging()
AddHandler _historicalLogging.AsyncLoggingRequestFinished, AddressOf _historicalLogging_AsyncLoggingRequestFinished
_historicalLogging.GetHistoricalData("LogCategory")
'I want to wait here and after the event raised, keep going
End Sub
Private Sub _historicalLogging_AsyncLoggingRequestFinished(ByVal sender As Object, ByVal e As AsyncRequestSuccessArgs)
RemoveHandler _historicalLogging.AsyncLoggingRequestFinished, AddressOf _historicalLogging_AsyncLoggingRequestFinished
For Each entry in _historicalLogging.Entries
'DoSomething
Next
End Sub
Note: HistoricalLogging is an object from a DLL. I cant change any code of that object
Not the best way to do it but it works
Private Sub GetLogs()
_historicalLogging = New HistoricalLogging()
AddHandler _historicalLogging.AsyncLoggingRequestFinished, AddressOf _historicalLogging_AsyncLoggingRequestFinished
_historicalLogging.GetHistoricalData("LogCategory")
'I want to wait here and after the event raised, keep going
Do Until EventRaisen
Application.DoEvents()
Loop
EventRaisen = False
'Code After The Event Has Raisen
End Sub
Dim EventRaisen As Boolean = False
Private Sub _historicalLogging_AsyncLoggingRequestFinished(ByVal sender As Object, ByVal e As AsyncRequestSuccessArgs)
RemoveHandler _historicalLogging.AsyncLoggingRequestFinished, AddressOf _historicalLogging_AsyncLoggingRequestFinished
For Each entry In _historicalLogging.Entries
EventRaisen = True
'DoSomething
Next
End Sub
I am completely lost here. I have dynamically created a menuitem. I've added an onclick-event handler, but this code never seems to fire. I remember it working a few months back and don't recall making any changes to it, but it's possibly something stupid that I've done.
Please see my code below:
frmMain._mnuSep1_0.Visible = True
Dim tlRecentApp As New ToolStripMenuItem(strMenuCaption)
tlRecentApp.Text = "Test"
tlRecentApp.Name = "AddApp"
tlRecentApp.Tag = strMenuID
RecentAppID = strMenuID
AddHandler tlRecentApp.Click, AddressOf Test
frmMain.mnuApplicantS.DropDownItems.Add(tlRecentApp.ToString)
The code for the Event:
Public Sub MnuRecentApp(ByVal sender As Object, ByVal e As EventArgs)
' MsgBox(sender.tag.ToString)
ApplicantID = sender.tag.ToString
frmApplicantEdit.Show()
End Sub
It gets created but when I click on it nothing happens:
If the code for the event handler is
Public Sub MnuRecentApp(ByVal sender As Object, ByVal e As EventArgs)
' MsgBox(sender.tag.ToString)
ApplicantID = sender.tag.ToString
frmApplicantEdit.Show()
End Sub
then this line
AddHandler tlRecentApp.Click, AddressOf Test
should be
AddHandler tlRecentApp.Click, AddressOf MnuRecentApp
Without trying to change too much of your code I've tested the following successfully:
Control:
Dim tlRecentApp As New ToolStripMenuItem(strMenuCaption)
'tlRecentApp.Text = "Test" This isn't needed as it's done on the above line when declared
tlRecentApp.Name = "AddApp"
tlRecentApp.Tag = strMenuID
RecentAppID = strMenuID
AddHandler tlRecentApp.Click, AddressOf MnuRecentApp
frmMain.mnuApplicantS.Items.Add(tlRecentApp)
Method:
Public Sub MnuRecentApp(ByVal sender As Object, ByVal e As EventArgs)
ApplicantID = CType(sender, ToolStripMenuItem).Tag.ToString
frmApplicantEdit.Show()
End Sub
mnuApplicantS is a ToolStrip control in my example. If you could clarify what mnuApplicatS is in your application I might be able to provide a better solution.
I found the problem. This line:
frmMain.mnuApplicantS.DropDownItems.Add(tlRecentApp.ToString)
Should read:
frmMain.mnuApplicantS.DropDownItems.Add(tlRecentApp)
Just In case anyone else has this problem.
I had an instance where it seemed my item click event was not firing.
It turned out I had removed the handler too early (it was dynamic menu).
I had removed the handler in the closed event of the Parent Menu (not kept open). It seems that parent close event is fired before the click event of the item is considered.
I have grid and those grid is populate on Form's load event. At the end line of that event i am hooking method handler for my SelectionChanged event of this grid. I want to get current selected row's zero cell's 1 value. Unfortunately when i run program my SelectionChanged event method handler is called infinite times... And i have no idea why is that.
So its basically like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
some code which populating data to grid...
'here hooking up method after data is there already to not fire it up during grid population
AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
End Sub
and this is event handler method itself:
Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs)
RemoveHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
ItemPanelImgs.Items.Clear()
'Dim images As New List(Of Article_Image)
Dim selectedNummer As String = String.Empty
selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()
'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
'ItemPanelImgs.DataSource = images
End If
AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
End Sub
P.S I am using to be concrete supergrid control from DotnetBar devcomponenets but it shouldn't be diffrent from ordinary controls behaviour.
What could be wrong here?
For those whom would like to debug here is sample app
EDIT:
I also tried this way but its still going to infinitive loop...
Public IgnoreSelectionChanged As Boolean = False
Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs) Handles gridArtikels.SelectionChanged
If IgnoreSelectionChanged Then Exit Sub
IgnoreSelectionChanged = True
If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
ItemPanelImgs.Items.Clear()
'Dim images As New List(Of Article_Image)
Dim selectedNummer As String = String.Empty
selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()
'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
'ItemPanelImgs.DataSource = images
End If
IgnoreSelectionChanged = False
End Sub
This is what I'm trying to do. I'm creating a dynamic check with an autoback that when clicked, will go to my subroutine and do something. The two parameters I'm trying to pass is the table which the checkox is located and the name of the id of the checkbox. But I'm getting the error
AddressOf must be the name of a method without parentheses or
method does not have a signature compatible with sender as object, e system.eventArgs". Here is my code below.
chkSel = New CheckBox
chkSel.ID = "check_" & CStr(a)
chkSel.AutoPostBack = True
'This is where I get the error
AddHandler chkSel.CheckedChanged, AddressOf change_operating_items(tableName, "check_" & CStr(a))
tblcell.Controls.Add(chkSel)
tblrow.Cells.Add(tblcell)
You cannot pass arguments when you register an event handler.
Instead you can pass them when you raise that event in case of a custom event.
Here you need to handle the CheckedChanged event, cast the Sender to CheckBox and use it's ID property.
Sub change_operating_items(sender As Object, e As EventArgs)
Dim chk = DirectCast(sender, CheckBox)
Dim id = chk.ID
' do something with it '
EndSub
I had to use the following to send both the sender, e and the custom parameters:
AddHandler chart1.PostPaint, Sub(sender, e) Chart1_BottomPostPaintScorePlot(sender, e, GroupNames)
Private Sub Chart1_BottomPostPaintScorePlot(ByVal sender As Object, ByVal e As ChartPaintEventArgs, ByVal GroupNames() As String)
If TypeOf e.ChartElement Is Legend Then
'process the legend at the bottom
End If
End Sub
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)