I guess it is a simple question but I researched here for other relevant topics that didn't work for me so here it is.
By pressing the specific button in Form2, Form1 appears, and then by pressing the button in Form1 the button in Form2 has to be pressed and show a message.I don't want to use a module with global variable because I have done it this way. Is there a way to get the specific instance of Form1 and not a new one? The 2 classes are in different .vb files, here is the code:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles form2Btn1.Click
Private Sub form2Btn2_Click(sender As Object, e As EventArgs) Handles form2Btn2.Click
Dim newForm1 As New Form1
Public Class Form1
Private Sub form1Btn_Click(sender As Object, e As EventArgs) Handles form1Btn.Click
Dim newForm2 As New Form2
Exposing form's controls or their event handlers to other classes not very good practice. But you can make public method which do same staff and then call it from other form.
Main idea in your solution is to pass existed/correct instance of
Form2 to the
Form1 as were suggested by @Plutonix in the comments.
So first extract logic from button_click eventhandler in
Form1 to the public method.
Public Class Form2 Public Sub ShowMessage() MsgBox("") End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles form2Btn1.Click ShowMessage() ' call the method here End Sub Private Sub form2Btn2_Click(sender As Object, e As EventArgs) Handles form2Btn2.Click 'Form implements IDisposable interface so 'it is good practice to wrap that instance with Using statement Using newForm1 As New Form1(Me) newForm1.ShowDialog() End using End Sub End Class
Create constructor in
Form1 which takes this instance as parameter
Public Class Form1 Private ReadOnly _Form1Instance As Form1 Public Sub New(form1Instance As Form1) _Form1Instance = form1instance; End Sub Private Sub form1Btn_Click(sender As Object, e As EventArgs) Handles form1Btn.Click 'Use instance of Form2 passed through constructor for calling public method _Form1Instance.ShowMessage() End Sub End Class