VB.NET Click Handler Error - vb.net

I have the following code:
CType(epuc, PropSoftware.SimpleUIControls.GenericPaymentControl).clickEvent = New EventHandler(AddressOf BtnAccept_Click)
and the following handler
Public Sub BtnAccept_Click(ByVal sender As Object, ByVal e As EventArgs)
But when I click the button, I'm not getting any response...which means the click event is not being triggered. Any idea why?

You need to use AddHandler.
http://msdn.microsoft.com/en-us/library/7taxzxka(v=vs.80).aspx#Y158
or WithEvents + Handles
http://msdn.microsoft.com/en-us/library/6k46st1y(v=VS.90).aspx

Use Addhandler:
AddHandler CType(epuc, PropSoftware.SimpleUIControls.GenericPaymentControl).clickEvent, AddressOf BtnAccept_Click

Related

"COM object that has been separated from its underlying RCW cannot be used" error related to vb.net form event

I'm hooking an arcobjects map event to a vb.net form to listen for map selection changes. This all works fine but users are reporting this error occassionally when opening the form. I can't see any pattern to reproduce the error and it seems to be random.
"COM object that has been separated from its underlying RCW cannot be used"
It originates from the form Load() method where I am hooking the event.
Can anyone help me understand what I've done wrong? I'm unhooking the map selection event in the FormClosing() event which I think is the correct approach.
Public Class MyForm
Private _activeViewEvents As IActiveViewEvents_Event
Private Sub FormLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_activeViewEvents = TryCast(pMxDoc.ActiveView.FocusMap, IActiveViewEvents_Event)
AddHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged
End Sub
Private Sub SelectionChanged
'do something when selection is changed
End Sub
Private Sub FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
RemoveHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged
End Sub
End Class
The approach you are taking to creating and destroying your handlers are valid. You can receive a RCW COM Exception when the map document is changed while your form is open. Since you are using the FocusMap to create the handles, when the document is changed, so is the FocusMap, which means you need to re-create your handlers for the new map document.
Ok so I think i've resolved this via use of the ActiveViewChanged event. Instead of rehooking the event on each form load or new document event, I tried listening for when the ActiveViewChanged event was fired and rehooking the SelectionChanged event each time. Turns out this is fired more than once each time a new document is opened (not sure why). Anyway, problem seems to have gone. Here's some example code:
Public Class MyForm
Private _activeViewEvents As IActiveViewEvents_Event
Private _docEvents As IDocumentEvents_Event
Private Sub FormLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler _docEvents.ActiveViewChanged, AddressOf ActiveViewChanged
End Sub
Private Sub ActiveViewChanged()
Dim maps = pMxDoc.Maps
For i = 0 to maps.Count - 1 'remove handlers from all maps
RemoveActiveViewEvents(maps.Item(i))
Next
SetupActiveViewEvent(pMxDoc.ActiveView.FocusMap) 'only add handler to active map
End Sub
Private Sub RemoveActiveViewEvents(map As IMap)
_activeViewEvents = CType(map, IActiveViewEvents_Event)
RemoveHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged
End Sub
Private Sub SetupActiveViewEvents(map As IMap)
_activeViewEvents = CType(map, IActiveViewEvents_Event)
AddHandler _activeViewEvents.SelectionChanged, AddressOf SelectionChanged
End Sub
Private Sub SelectionChanged
'do something when selection is changed
End Sub
End Class

VB.Net Menuitem added dynamically not firing click event

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.

A function is called twice in VB.NET

I have a function that is called twice and I don't know what to do.
This is the code that is called when I press an input button on a WebBrowser:
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
Private Sub Document_Click(sender As Object, e As HtmlElementEventArgs)
Select Case Document.ActiveElement.Id.ToLower
Case "global" : prueba()
Case Else
End Select
End Sub
If you want to see the function called prueba() here it is: http://pastebin.com/Fi5LLX2N
I have a video where I show it, but the annotations are in Spanish: http://www.youtube.com/watch?v=OCJXk3qJwVA
Well I have another problem with my function, as you can see, on the bottom I have put this:
Else
MsgBox("Este ModPack ya lo tienes instalado!")
End If
But it doesn't work. :(
Try this:
PS: It's written on the fly maybe can have some syntax error.
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
try : removehandler Document.Click, addressof(Document_Click): catch : end try
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
My immediate reaction is a sticky mouse button, but really it's likely due to the web page you are loading having multiple pages being loaded, thus adding the duplicate event handler. Put a breakpoint on this line of code:
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
You'll likely see it being hit twice. Make sure to only wire up one HtmlElementEventHandler to avoid the double firing of the click event handler. You can check e.Url for a match before wiring up as a possible solution.
in vb.net there is no need to define onclick in html for button because it is handled automatically.so if you are doing this then the click event will fire twise.

Click button to trigger multiple button clicks

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.

How do I fire an event in VB.NET code?

I have a form that has a start button (to allow users to run the processes over and over if they wish), and I want to send a btnStart.Click event when the form loads, so that the processes start automatically.
I have the following function for the btnStart.Click event, but how do I actually tell Visual Basic 'Pretend someone has clicked the button and fire this event'?
I've tried going very simple, which essentially works. However, Visual Studio gives me a warning Variable 'sender' is used before it has been assigned a value, so I'm guessing this is not really the way to do it:
Dim sender As Object
btnStart_Click(sender, New EventArgs())
I have also tried using RaiseEvent btnStart.Click, but that gives the following error:
'btnStart' is not an event of 'MyProject.MyFormClass
Code
Imports System.ComponentModel
Partial Public Class frmProgress
Private bw As BackgroundWorker = New BackgroundWorker
Public Sub New()
InitializeComponent()
' Set up the BackgroundWorker
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
' Fire the 'btnStart.click' event when the form loads
Dim sender As Object
btnStart_Click(sender, New EventArgs())
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
If Not bw.IsBusy = True Then
' Enable the 'More >>' button on the form, as there will now be details for users to view
Me.btnMore.Enabled = True
' Update the form control settings so that they correctly formatted when the processing starts
set_form_on_start()
bw.RunWorkerAsync()
End If
End Sub
' Other functions exist here
End Class
You should send a button as sender into the event handler:
btnStart_Click(btnStart, New EventArgs())
Steps in involved in raising an event is as follows,
Public Event ForceManualStep As EventHandler
RaiseEvent ForceManualStep(Me, EventArgs.Empty)
AddHandler ForceManualStep, AddressOf ManualStepCompletion
Private Sub ManualStepCompletion(sender As Object, e As EventArgs)
End Sub
So in your case, it should be as below,
btnStart_Click(btnStart, EventArgs.Empty)
Just Call
btnStart.PerformClick()
You are trying to implement a bad idea. Actually, you have to make a subroutine to accomplish these kind of tasks.
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
call SeparateSubroutine()
End Sub
private sub SeparateSubroutine()
'Your code here.
End Sub
And then whereever you want to call the btnStart's click event, just call that SeparateSubroutine. This should be a correct way in your case.
You can subclass the button and make its OnClick Method public as I described here.