subject is declare event on Form1 , and run that event on Form2 here is the code , but event doesnt work on Form2 !! what is missing here?? many thanks
Form1:
Public Class Form1
Private Sub btnOpenForm2_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
Form2.Show()
End Sub
Public Event show_My_Message()
Private Sub btnShowMessageOnForm2_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
RaiseEvent show_My_Message()
End Sub
End Class
Form2:
Public Class Form2
Public WithEvents My_Form1 As Form1 = New Form1
Private Sub Show_My_Message_On_Form2() Handles My_Form1.show_My_Message
MsgBox("Hello")
End Sub
End Class
CodePudding user response:
Here's an example of what Jimi was talking about, using the .Owner
property.
Form1:
Public Class Form1
Private Sub btnOpenForm2_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
Form2.Show(Me) ' <-- Pass Form1 into Form2 via Show()
End Sub
Public Event show_My_Message()
Private Sub btnShowMessageOnForm2_Click(sender As Object, e As EventArgs) Handles btnShowMessageOnForm2.Click
RaiseEvent show_My_Message()
End Sub
End Class
Form2:
Public Class Form2
Private WithEvents My_Form1 As Form1
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If Not IsNothing(Me.Owner) AndAlso Me.Owner Is Form1 Then
My_Form1 = DirectCast(Me.Owner, Form1)
End If
End Sub
Private Sub F1_show_My_Message() Handles My_Form1.show_My_Message
MessageBox.Show("Hello")
End Sub
End Class
CodePudding user response:
Try it without cross form event handling,
Form1
Public MyForm2 As Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If MyForm2 Is Nothing Then
MyForm2 = New Form2
End If
MyForm2.Show()
MyForm2.Show_My_Message_On_Form2("Hello")
End Sub
Form2
Public Class Form2
Public Sub Show_My_Message_On_Form2(theMess As String)
MessageBox.Show(theMess)
End Sub
End Class