Raise an event on another form - vb.net

I have two separated form(usercontrol A & B) in VB.Net. When a button clicked on formB, I want to an event raised on formA. How can do that? I searched stackoverflow.com and check several related solutions but they didn't work for me. I don't know what is the problem! Please help me.
For Example I tried this code:
FormA:
Dim MyEditForm As New ucEmployeeTimeEdit
'some code for showing and preparing MyEditForm
Private Sub GetSomeClick() Handles MyEditForm.MyClick
System.Console.WriteLine("test")
End Sub
FormB:
Public Event MyClick()
Private Sub BtnDelet_Click(sender As Object, e As EventArgs) Handles btnDelet.Click
RaiseEvent MyClick()
End Sub

While Idle_Mind answer is great, here's an alternative method with different advantages. (EDIT: I just noticed that he mentions this too, so you can consider this an elaboration on this subject)
If you keep FormB as a modal variable inside FormA, you can tell VB that you want to use it's events. Here's some skeleton code to show you:
Public Class FormA
Private WithEvents myFormB As FormB
'rest of the code for FormA
'you can use FormB's events like this
Private Sub GetSomeClicks() Handles myFormB.MyClick
'code code code
End Sub
End Class
Public Class FormB
Public Event MyClick()
End Class
Have fun!

Somewhere in the some code portion, you need to use AddHandler to "wire up" that event:
Public Sub Foo()
Dim MyEditForm As New ucEmployeeTimeEdit
'some code for showing and preparing MyEditForm
AddHandler MyEditForm.MyClick, AddressOf GetSomeClick
End Sub
Private Sub GetSomeClick()
System.Console.WriteLine("test")
End Sub
Note that GetSomeClick() does not have a "Handles" clause on the end. This is because your instance of ucEmployeeTimeEdit was a local variable.
If you had declared it as WithEvents at class level, then you could use the "Handles" clause (and consequently would no longer need the AddHandler call).

Related

VSTO Outlook Add-in: Cannot use Explorer_Activate event due to ambiguity

I am writing an Outlook 365 32 bit VSTO add-in that can perform code when the to-do list explorer is activated. Ideally, I would simply do something like the following:
Private WithEvents OlExplr As Outlook.Explorer
Private Sub ThisAddIn_Startup() Handles Me.Startup
OlExplr = Application.ActiveExplorer
End Sub
Private Sub OlExplr_Activate() Handles OlExplr.Activate
'''Do some stuff here
End Sub
Unfortunately, Activate is both a method and event for the Explorer class, and as such, compile errors will occur if I attempt to implement as above. I have seen several examples (below) of how to handle this ambiguity in C#, but I cannot translate to vb.net effectively, nor do I really understand what they mean by "cast OutlookExplorer variable above to ExplorerEvents interface". I know I will need to use ExplorerEvents_10_ActivateEventHandler or possibly ExplorerEvents_10_Event, but the actual implementation is beyond my current skill level.
VSTO Outlook AddIn: Cannot use Explorer Close event
What is the difference between _Application and Application
Can someone please explain what casting would mean in this context, and how to circumvent the ambiguity issue?
Edit:
Following Dmitry's answer, I got the following, though it is not triggering the event still (no errors are being flagged at least...)
Public Class ThisAddIn
Private WithEvents OlExplr As Microsoft.Office.Interop.Outlook.Explorer
Public Delegate Sub ExplorerEvents_10_ActivateEventHandler(ByRef sender As Object)
Public Event OlExplr_Activate As ExplorerEvents_10_ActivateEventHandler
Private Sub ThisAddIn_Startup() Handles Me.Startup
OlExplr = Application.ActiveExplorer
AddHandler OlExplr_Activate, New ExplorerEvents_10_ActivateEventHandler(AddressOf OlExplr.Activate)
End Sub
Private Sub ThisAddIn_OlExplr_Activate(ByRef sender As Object) Handles Me.OlExplr_Activate
MsgBox("Hello!")
End Sub
Instead of statically declaring the event handler, try to use AddHandler statement dynamically at run-time.
Off the top of my head (I don't use VB.Net):
Private Sub ThisAddIn_Startup() Handles Me.Startup
OlExplr = Application.ActiveExplorer
AddHandler (OlExplr As ExplorerEvents).Activate, AddressOf OlExplr_Activate
End Sub
Private Sub OlExplr_Activate()
'''Do some stuff here
End Sub
Using what Dmitry posted, I was able to tweak the syntax to capture and fire the event:
Private WithEvents OlExplrs As Outlook.Explorers
Private WithEvents Explr As Microsoft.Office.Interop.Outlook.Explorer
Private Sub ThisAddIn_Startup() Handles Me.Startup
OlExplrs = Application.Explorers
End Sub
Private Sub OlExplrs_NewExplorer(Explorer As Explorer) Handles OlExplrs.NewExplorer
If Explorer.Caption = "To-Do List..." Then
Explr = OlExplrs.Item(Explorer.Caption)
End If
AddHandler Explr.Activate, AddressOf MyActivateHandler
End Sub
Private Sub MyActivateHandler()
'''Do stuff...
End Sub
It turns out, however, that the way I have Outlook set up, the Activate method is not triggered when the desired explorer window (in this case, the to-do list) is brought into focus. The Activate method only fires if the explorer goes from a minimized to a maximized state or gets initialized and opened.
The Hide/Unhide event (or whatever it is that is happening when moving from the inbox window to the to-do list window and back, etc...) is really what I would need. This does not exist as a built in event for the explorer, so perhaps a custom event could work? I will open a separate question if I cannot find a solution.

declared event which fires, but is not heard

I have a vb.net application which contains two forms. One is called "MapComponentForm" and another called "ComponentPropertiesForm". In the MapComponentForm I have defined an event which I want fired when the OK button is clicked. If the ComponentPropertiesForm is open, I want it to "hear" this event and act accordingly. Stepping through the code the event seems to fire as expected but the ComponentPropertiesForm seems to be oblivious to this. I've attached the code from the two forms after stripping out the non relevant code in hopes that someone can tell me why my event goes unheeded. I used the information here:
https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/events/walkthrough-handling-events
in constructing my own code.
Thanks for any suggestions.
Public Class MapComponentForm
Public Event PartMapped(ByRef status As Boolean)
Public Sub New(shape As Visio.Shape)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_shape = shape
End Sub
Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
'If the component properties window is open, refresh it
If Application.OpenForms().OfType(Of ComponentPropertiesForm).Any Then
RaiseEvent PartMapped(True)
End If
end sub
end class
Public Class ComponentPropertiesForm
Private WithEvents mPartMap As MapComponentForm
Private Sub ComponentPropertiesForm_Load(sender As Object, e As EventArgs) Handles Me.Load
mPartMap = New MapComponentForm(Nothing)
End Sub
Private Sub mPartMap_PartMapped(ByRef status As Boolean) Handles mPartMap.PartMapped
If status = True Then
MsgBox("something got mapped")
End If
End Sub
end class

Can't call form_Load event because of "not declared" error, but the name is correct and the event is in the appropriate class

I'm trying to add a load event to a form so that the data on the form (pulled from a DB), will refresh after the user performs an add, delete, or update. Each of these functions is accessed by a click event. The form_Load event is the last line of code in the subroutine calling the click event.
I've made sure the name of the form matches the name in the Load event call, and I've made sure that the form isn't still named "Form1" elsewhere the code.
Here is the code used for each of my Load event calls:
Option Strict On
Public Class frmHome
Private Sub mySub
' do something
'Refresh frmHome to show new customer
frmHome_Load(sender, e)
End Sub
Here is the error message:
Error BC30451 'frmHome_Load' is not declared. It may be inaccessible due to its protection level.
Here's an example of what Jimi is talking about:
Public Class frmHome
Private Sub frmHome_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Foo()
End Sub
Private Sub Foo()
' ... code ...
End Sub
Private Sub mySub()
' do something
Foo()
End Sub
End Class

Unable to handle event in another class

I am kinda new to visual basic and was wondering if it is possible to trigger events in a class, based on action with in form.
So the following is my problem:
I have a form called Main, with a picture box called picPID
And a class called add_new
What I want to do is:
when the right mousebutton is clicked, the code for the event handling is placed within the class called add_new
I thought i could just declare it in the following way:
Sub meMouseDown(sender As Object, e As MouseEventArgs) Handles Main.picPID.MouseDown
But I get an error saying i have to declare it withEvents, I tried to declare the picturebox as:
Public shared withEvents as picturebox
but it dident help, any sugestions? It is not a problem having the code within the Main form, but it would result in a lot of code, so I was hoping to split it up.
So you have the class with an eventhandler:
Public Class add_new
Public Sub PictureBoxEventHandler(sender As Object, e As MouseEventArgs)
'Your Implementation
End Sub
End Class
Then you need an instance of this class within the form containing the picturebox:
Public Class Form1
Private add_New_Command As New add_new ' hold a reference to the command
'constructor
Public Sub New()
InitializeComponent()
' and add the handler to the event of the picture box ... hook it up
AddHandler PictureBox1.MouseDown, AddressOf add_New_Command.PictureBoxEventHandler
End Sub
End Class

Passing event to the parent form

I have a little problem here. I'm trying to transfer/pass/raise the events of an owned form to his parent. Lets look at my example:
Lets say i have a form that initialize a CustomPanel (simply a class that inherits from System.Windows.Forms.Panel). It also have an event handler (it could be an other event, not necessarily a click event):
Public Sub New()
Me.Size = New Size(1000,1000)
Dim pnl1 As New CustomPanel()
pnl1.Location = New Point(0,0)
pnl1.size = New Size(100,100)
Me.Controls.Add(pnl1)
End Sub
Private Sub form1_Click(sender As Object, e As EventArgs) Handles Me.Click
MsgBox("I got it!")
End Sub
I did something similar and when I clicked on the CustomPanel (pnl1) the parent container (form1) did not receive a click event ... which is understandable. I tried to look in the properties of the CustomPanel (pnl1) if i could find something interesting like "click through" or "raise event to parent" (I was desperate here) but without success. I said alright, I will handle the events that I need to pass to parent in the CustomPanel class but I cant find a solution here neither:
Imports System.Windows.Forms
Public Class CustomPanel
Inherits Panel
Public Sub New()
End Sub
Private Sub CustomPanel_Click(sender As Object, e As EventArgs) Handles Me.Click
'What to put here?
'Me.Parent.?
End Sub
End Class
I just want to know if its possible to throw/raise/pass events to the parent. One thing is sure, its that i shouldn't have to and i cannot add anything else to the parent form. The reason is simple, i could have over 100 controls in this parent form and they could be added dynamically. And on top of that, these controls could also have their own controls inside! So i could have something like:
pnl99 call parent click -> pnl98 call parent click -> ... until the parent of the control really handle the click event ... -> form1 perform click event
Maybe its hard to understand but if you can help me I would appreciate.
Using a custom event, that the form owning the panel subscribes to. Raise Event
Public Sub New()
Me.Size = New Size(1000,1000)
Dim pnl1 As New CustomPanel()
pnl1.Location = New Point(0,0)
pnl1.size = New Size(100,100)
Addhandler pnl1.MyClickEvent, AddressOf pl_Click
Me.Controls.Add(pnl1)
End Sub
Private Sub pl_Click()
MsgBox("I got it!")
End Sub
Custom panel:
Public Class CustomPanel
Inherits Panel
Public Event MyClickEvent
Private Sub CustomPanel_Click(sender As Object, e As EventArgs) Handles Me.Click
RaiseEvent MyClickEvent()
End Sub
End Class
On the child form
Imports System.Windows.Forms
Imports STAS_PLC_Link_Lib
Public Class ChildForm
Public Event MyClick()
'.....rest of code
On the parent form
Public Class ParentForm
Private Sub GetSomeClick() Handles ChildFor.MyClick
System.Console.WriteLine("test")
End Sub
end Class