Calling Subroutines in Form1 from Form2

johnadonaldson

Well-known member
Joined
Nov 9, 2005
Messages
48
Programming Experience
10+
Now I have a different but similar problem.

I want the second window to call a subroutine in the main form.

In the second form I setup

Private fOMain as Form1

PrivateSub TermSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TermSend.Click
fOpMain.SendRS232()
EndSub

if Form1 I have

PublicSub SendRS232()
Me.TextBox2.Text = "hello"
EndSub

When I click the TermSend button nothing happens on Textbox2 on the main form.


 
narrowed it down to the form2 routine

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fOpMain As Form1
Try
fOpMain.SendRS232()
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to write to Main Form")
End Try
End Sub

It pops the exception everytime.


 
add a module to the progect and in this module put:
VB.NET:
Friend MainForm As Form1  'where Form1 is the main form

then in the main form's load event put:
VB.NET:
MainForm = Me

now in any of the form's if you need to call a sub/function simply use MainForm.Sub in this case it would be MainForm.SendRS232
 
Back
Top