Click button to trigger multiple button clicks - vb.net

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.

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

trigger an event from another control

I'm using vb.net and winform. I am coming across an issue which I'm pounding my head against for the past few hours.
I have a main usercontrol which I added a groupbox and inside that groupbox, added a control like this:
main usercontrol
Me.GroupBox1.Controls.Add(Me.ctlWithDropDown)
user control ctlWithDropDown
Me.Controls.Add(Me.ddList)
Private Sub ddlList_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlList.SelectionChanged
'some simple logic here to check if value changed
End Sub
The main usercontrol inherits the base class which has an event to set a value to true or false like so:
Public Event SetFlag(ByVal value As Boolean)
I want to know how I can trigger/set this boolean value from the dropdownlist when the SelectionChanged event is trigger. Any help on this issue?
Wire up an event handler for the drop down list:
AddHandler Me.ctlDropDown.SelectedIndexChanged, AddressOf ddlSelectedIndexChanged
Me.GroupBox1.Controls.Add(Me.ctlDropDown)
Make sure you create ddlSelectedIndexChanged in your control and have it fire the SetFlag Event:
Protected Sub ddlSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent SetFlag(True)
End Sub
I presume the me.ctlDropDown is something that you are making programmatically? If so then this sort of thing should work for you.
Public Sub Blah()
Dim ctlDropDown As New ComboBox
AddHandler ctlDropDown.SelectedIndexChanged, AddressOf IndexChangedHandler
Me.GroupBox1.Controls.Add(ctlDropDown)
End Sub
Private Sub IndexChangedHandler()
'Do whatever you need here.
End Sub
However, if this is not created at runtime should make an event handler like:
Private Sub IndexChangedHandler() Handles Me.ctlDropdown.SelectedIndexChanged
'Do whatever you need here.
End Sub

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.

VB.NET Click Handler Error

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

Textbox click event in vb.net

I am trying to write an onclick event for textbox but VB.net does not seem to support textbox1.click()
I want to open a new form every time someone clicks on the textbox.
Opening a new form is no problem, but I cant detect the click.
Is there any event for textbox that detects click event?
I saw something like TextboxBase that has Click but I am able to use it well.
please help!
This is how my class looks :
Partial Public Class TextBoxClick
Inherits System.Web.UI.Page
End Class
It has some basic load and init events.
I am trying to write a Sub like this :
Private Sub incident_clicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles Incident.OnClick
Incident.Click does not work either.
I am guessing I need to import some class to access the Click event but I am not sure which.
Thanks is advance
TextBox has a Click event, using it is no problem. Your Handles clause however uses OnClick, that's not a valid event name. Do make sure this Sub is inside a Form class and not a module.
Public Class Form1
Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
MessageBox.Show("Click!")
End Sub
End Class
You could use onFocus event :)
According to MSDN, your code should work as follows:
Private Sub TextBox1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TextBox1.Click
' Code to handle the event here
End Sub
However, you could also try the MouseUp event:
Private Sub textbox1_MouseUp(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) _
Handles textbox1.MouseUp
' Code to handle the event here
End Sub
' Will fire if textbox gets focused
Private Sub incident_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles incident.GotFocus
Debug.Print("inciden got focus")
End Sub
' Will fire if textbox gets mouse clicked
Private Sub incident_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles incident.MouseClick
Debug.Print("inciden got clicked")
End Sub
For anyone who is having trouble with this, I fixed it by switching to an asp control. My button now looks like this:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
Not sure why, but I now have a working click event.
For me the Textbox_ click event is triggered only when I type a character in that box.
The textbox click event is triggered only when you type a character in that textbox.
That is disgusting. You may want to try mouse-enter mouse-leave events they are more reliable. Babu V Bassa.