Results 1 to 14 of 14

Thread: Accessing subroutines from another form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Accessing subroutines from another form

    Hello,

    I was wondering if there's a way to access a subroutine from another form?

    Before in VB6, all you need to do is form2.cmdClear_Click()

    or something..... is there a vb.net equivalent to this?


    Thanks,

    Chris

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    If I understand correctly, what you want to do is pretty easy. If it's a shared (static) method, you can just call it without creating an instance of that object first. If it's not a shared method, you need an instance of the object before you can call one of it's methods.

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    This code is in Form2:

    VB Code:
    1. Shared Sub Hello1()
    2.         MessageBox.Show("Hello1")
    3.     End Sub
    4.  
    5.     Sub Hello2()
    6.         MessageBox.Show("Hello2")
    7.     End Sub

    This code is in Form1:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Form2.Hello1()
    3.     End Sub
    4.  
    5.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    6.         Dim f As New Form2
    7.         f.Hello2()
    8.     End Sub

    You can call a shared method without first creating an instance of Form2, but if the method is not shared, you need an instance first. If you tried to call Form2.Hello2(), you'd get an error that says reference to a non-shared method requires an object reference.

    HTH,
    Mike

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    This small project has an example of how to call a form. Its not exactly what your looking for per se, but you can probly work things out looking at it. You need to learn the differences between object instances and declarations, its one of the major differences between vb6 and .net and had been covered many many times in these forums.

    http://www.vbforums.com/attachment.p...postid=1600084

    btw, try searching.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I think we all may be answering different questions here.

    If you want to access procedures or other routines in Form2 from Form1, then, unless they are declared as Shared,you must ensure that you have instanced Form2. However, I do not think you can declare an object's event as Shared and achieve this. Remember that in .NET a form does not exist in your application until you have created an instance of it. This was not so in VB6.

    If you are looking for the method to access the Click event of another object it is:

    form2.cmdClear.PerformClick

    but remember that you must create an instance of Form2 before you can do this.
    Last edited by taxes; Jan 24th, 2004 at 08:49 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    Addicted Member
    Join Date
    Apr 2002
    Location
    California
    Posts
    160
    Yeah this is easy. You can do something like:

    VB Code:
    1. Public Shared Function AccessForm1Name(ByVal sender As Object) As String
    2.         Return "The Caller was " & sender.GetType.ToString & " Accessing " & "Form1"
    3.     End Function

    Put this in form1, and call it from form2. By using sender in the args you can determine what class called the method.
    Jason Moore

    Software Engineer, Database Architect, Web Designer

    (C#,VB/NET,ASP/NET,COLDFUSION,JAVASCRIPT,SQL)

    http://www.gatorstudios.com
    [email protected]

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Jason,

    How would you apply this to activate the Click event of a button?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If it's not eventhandler (related to any control) , then put it in a module or class file shared flaged .

  9. #9
    Addicted Member
    Join Date
    Apr 2002
    Location
    California
    Posts
    160
    just change the button click sub to a Public Shared Sub:

    VB Code:
    1. Public Shared Sub Button1_Click(ByVal Sender as Object, ByVal e as System.EventArgs) Handles Button1.click
    2.  
    3. End Sub

    Now you can simulate the button being clicked from another form.
    Jason Moore

    Software Engineer, Database Architect, Web Designer

    (C#,VB/NET,ASP/NET,COLDFUSION,JAVASCRIPT,SQL)

    http://www.gatorstudios.com
    [email protected]

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Jason,

    Sorry but I can't get this to work. What code do you place in the calling procedure/event?

    I tried:

    form2.button1_click

    and

    form2.button1.performclick

    without success.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    Addicted Member
    Join Date
    Apr 2002
    Location
    California
    Posts
    160
    OK place following code in Form1 and replace the word Button1 with whatever your button name is:

    VB Code:
    1. Public Shared Sub Button1_Click(ByVal Sender as Object, ByVal e as System.EventArgs) Handles Button1.click
    2.  
    3. End Sub

    In Form2 use this in your procedure:

    VB Code:
    1. Form1.Button1_Click
    Jason Moore

    Software Engineer, Database Architect, Web Designer

    (C#,VB/NET,ASP/NET,COLDFUSION,JAVASCRIPT,SQL)

    http://www.gatorstudios.com
    [email protected]

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Jason,

    I have already done all that.

    When I enter In Form2 procedure:


    form1.Button1_Click

    I get the intellisense comment;

    Argument not specified for parameter 'e' of 'Public Shared Sub Button1_Click(Sender as object, e as system.EventArgs)'.

    On running, a build error as above is displayed together with a second build error of:

    : Argument not specified for parameter 'sender' of 'Public Shared Sub Button1_Click(sender As Object, e As System.EventArgs)'.


    but the application will continue and the startup form appears. The procedure in form 2 does NOT implement the Click method of the button in form1.

    HOWEVER, something very funny is happening!!

    I tried putting a Public Shared sub in form1 which placed a string in a textbox on form2 and called it from form2. This, of course, worked perfectly BUT I then deleted that sub and tried again. The string which WAS included in the form1 sub still appeared in the form2 textbox - even though all the code had been deleted. I tried rebuilding the projectand also exiting VB.NET completely but this behaviour continues. It was just as though I have never removed the code at all! Also, although I could amend the base form2 by adding additional text boxes and buttons, these additions would not appear in the instantiated form2 (named form22) at runtime.I eventually got round this by deleting the calling sub completely and then re-entering it. This restored the expected behaviour, including the proper appearance of the instantiated form2.

    Have you ever run into this?

    Many thanks for your help.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    Addicted Member
    Join Date
    Apr 2002
    Location
    California
    Posts
    160
    My bad bro, its Form1.Button_Click(me,nothing)

    God! I cant believe i had you going in circles on that. Well, guess you learn a little bit more with every problem.

    me = the object that is calling the function, in this case it would be the form class name in which you placed that function call.

    nothing - Means you are not sending any event args with the call.

    Sorry.
    Jason Moore

    Software Engineer, Database Architect, Web Designer

    (C#,VB/NET,ASP/NET,COLDFUSION,JAVASCRIPT,SQL)

    http://www.gatorstudios.com
    [email protected]

  14. #14
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Jason,

    Got it!!!

    Thanks a million for your effort.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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