|
-
Dec 8th, 2003, 04:41 AM
#1
Thread Starter
Lively Member
Accessing a Sub Routine from another form.
I have two forms in my solution. One is called frmMain and the other is called frmApplications. frmMain is the first one that starts up and it declares a new object referencing frmApplications. When I want to show frmAppications I use fApps.Show (Note: fApps is a object reference to frmApplications). I can access frmApplications methods by using the fApps object. But the problem is that I can't seem to figure out how to reference frmMain from frmApplications. I want to do this because frmMain has a Sub Routine I want to run on it, and I want to be able to run this sub from frmApplications. I'm not sure how this is done in VB .NET. Does anyone know what I'm doing wrong?
I'm sure it's simple.
-
Dec 8th, 2003, 04:45 AM
#2
Addicted Member
one way is to send an instance of your main form to second forms constructor, then call the method using the instance
-
Dec 8th, 2003, 04:45 AM
#3
Thread Starter
Lively Member
Can you explain this operation a little more in detail?
-
Dec 8th, 2003, 04:54 AM
#4
Addicted Member
first add this constructer to your second form;s code
Code:
Dim mainform As Form1
Public Sub New(ByVal mainform As Form1)
Me.mainform = mainform
End Sub
then when instanciating form2 from form1 ( your main form), use the new overloaded constructer you created , it would look like this >>
Code:
Dim frm2 As New Form2(Me)
then you can call the main forms procedures with the 'mainform' instace you have
-
Dec 8th, 2003, 05:05 AM
#5
Thread Starter
Lively Member
Is this the standard way of doing things or did you just adapt the code to meet my needs. What is the standard way of starting a multiform application in VB.NET? I ask this because I am not quite sure I understand the code you posted.
-
Dec 8th, 2003, 05:09 AM
#6
Addicted Member
i think this is how it should be done in .net, passing objects around to use there members .
where in the code do you not understand?!
-
Dec 8th, 2003, 05:10 AM
#7
Thread Starter
Lively Member
Sorry about that, I think I got it. Thanks for all your help. I couldn't find info anywhere.
-
Dec 8th, 2003, 05:12 AM
#8
Thread Starter
Lively Member
Actually can you explain the second part of your code to me more in depth?
-
Dec 8th, 2003, 05:27 AM
#9
Thread Starter
Lively Member
I got it, thanks buddy for all your help. What is your name so that I can add you to the credits?
-
Dec 8th, 2003, 05:32 AM
#10
Addicted Member
you create an object just as you did before, this time you also pass a parameter to the constructor,'me' is a reference to the form1 object ( its a keyword in vb ) you pass it the second form. then in the second form you can use the first forms members using the passed object which we stored it in a variable named 'mainform' , so if you have a procedure name "method1" in form1 you can call it in the second form like
mainform.method1()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|