Results 1 to 10 of 10

Thread: Accessing a Sub Routine from another form.

  1. #1

    Thread Starter
    Lively Member D.Viddy's Avatar
    Join Date
    May 2003
    Location
    Soldotna, Alaska
    Posts
    108

    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.
    ~Dylan
    ~http://www.codebend.net
    ~The place for Internet Development help.

  2. #2
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    one way is to send an instance of your main form to second forms constructor, then call the method using the instance

  3. #3

    Thread Starter
    Lively Member D.Viddy's Avatar
    Join Date
    May 2003
    Location
    Soldotna, Alaska
    Posts
    108
    Can you explain this operation a little more in detail?
    ~Dylan
    ~http://www.codebend.net
    ~The place for Internet Development help.

  4. #4
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    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

  5. #5

    Thread Starter
    Lively Member D.Viddy's Avatar
    Join Date
    May 2003
    Location
    Soldotna, Alaska
    Posts
    108
    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.
    ~Dylan
    ~http://www.codebend.net
    ~The place for Internet Development help.

  6. #6
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    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?!

  7. #7

    Thread Starter
    Lively Member D.Viddy's Avatar
    Join Date
    May 2003
    Location
    Soldotna, Alaska
    Posts
    108
    Sorry about that, I think I got it. Thanks for all your help. I couldn't find info anywhere.
    ~Dylan
    ~http://www.codebend.net
    ~The place for Internet Development help.

  8. #8

    Thread Starter
    Lively Member D.Viddy's Avatar
    Join Date
    May 2003
    Location
    Soldotna, Alaska
    Posts
    108
    Actually can you explain the second part of your code to me more in depth?
    ~Dylan
    ~http://www.codebend.net
    ~The place for Internet Development help.

  9. #9

    Thread Starter
    Lively Member D.Viddy's Avatar
    Join Date
    May 2003
    Location
    Soldotna, Alaska
    Posts
    108
    I got it, thanks buddy for all your help. What is your name so that I can add you to the credits?
    ~Dylan
    ~http://www.codebend.net
    ~The place for Internet Development help.

  10. #10
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    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
  •  



Click Here to Expand Forum to Full Width