Results 1 to 14 of 14

Thread: Call Public Subs from other forms

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Karlsruhe, Germany
    Posts
    8

    Call Public Subs from other forms

    Hi,
    I'm just switched from VB 6 to VB.Net and tried to use a public sub at the MainForm from a second sub form. But it didn't appears in the property/method contents of the MainForm-form. So what do I have to do make the sub accessable from within other forms?
    Thanks for your help.

    Gunnar

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Put the sub in a class and call the class from both forms

    dim blah as New Class1()

    blah.MySub()
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    What's wrong with just putting the code in a module?
    ~Peter


  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Don't know if it's some heinous .NET sin but I am also fond of sticking cross-form stuff in modules (nod to edneeis)

  5. #5
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Originally posted by Slow_Learner
    Don't know if it's some heinous .NET sin but I am also fond of sticking cross-form stuff in modules (nod to edneeis)
    No, its not a sin! If it makes your code easier to write/read/modify, then its what you should be doing!

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Karlsruhe, Germany
    Posts
    8
    Ok, when I put the function into a module it can be used in both forms, but this function needs a control on form1 that's why I tried to use functions of form1 in form2, so I can start a function in the module but this function isn't able to do it's job.
    Why should I generate a new object by using dim blah as new class()...? A public sub/function in an object have to be usable by any other objects no matter who generates this object and where this happens. When the program starts there is already an object of type Class, when I use the dim new... I end up with two form1's of the type class...

    Gunnar

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If it uses a control on form1 then you'll have to pass the control into it.

    VB Code:
    1. 'in module or other class
    2. if its in a class make it shared
    3. Public Sub ChangeText(ByVal newtext as string,ByRef txtbox as TextBox)
    4.   txtbox.Text=newtext
    5. End Sub
    6.  
    7. 'call from form1
    8. ChangeText("Something New",Me.TextBox1)
    9. 'or if its in a class
    10. ClassName.ChangeText("Something New",Me.TextBox1)

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    another way to do it would be to have a setcontrol sub in the module that sets the module level control to the one of form1 and gets used in the changetext sub for all forms or what not that use it.

    VB Code:
    1. 'in module
    2.  
    3. Public useTextBox as TextBox
    4.  
    5. Public Sub ChangeText(ByVal newText As String)
    6.   If Not useTextBox Is Nothing Then
    7.      useTextBox.Text=newText
    8.   End If
    9. End Sub
    10.  
    11. 'then set it in form1_load
    12. useTextBox=Me.TextBox1
    13.  
    14. 'then anyone can call it like this
    15. ChangeText("Something Newer")

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Karlsruhe, Germany
    Posts
    8
    Hi,
    I used your second example and it works perfect. Thanks a lot.
    But I can't imagine there is another way to address the control. If I'm in a sub in Form2 (while the ActiveX Control is in Form1) then Me. will address all controls and functions in Form2, is there another level above Me so I can address in a way like OverMe.Form1.ActiveXControl.Method?

    Gunnar

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Karlsruhe, Germany
    Posts
    8
    Concerning your first solution, that was what I tried, but when there's a public sub in Form1 and I try to call it in Form2 with command Form1.Subname there's no such method of Form1.

    Gunnar

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It should work fine. I think the trouble you may be having is the naming and seperating an instance from the main class. If the form class is named Form1 then you wouldn't call it like Form1.MyMethod you have to use the instance that has been created and passed around like:

    VB Code:
    1. 'in Form1 if you make an instance of Form2
    2. dim frm as New Form2
    3.  
    4. 'then you do NOT do
    5. Form2.MyMethod()
    6.  
    7. 'you use the instance
    8. frm.MyMethod()

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Karlsruhe, Germany
    Posts
    8
    Hi,
    yes but with this I have two problems.

    No.1 - When the program starts there's already an instance of
    the MainForm created because of starting the program, but I don't know what's the name of this object is. So when I load in SubForm (Form2) in the program I have no idea how to address to MainForm object that already exists. And generating a new object by calling dim FormName as new MainForm in the SubForm would create a second object of type MainForm but I neither want nor need a second object of that type.

    No.2 - I generated the SubForm with dim Form2 as new SubForm, but when I try to address functions with Form2.method, Form is unknow, when I use SubForm.method it works with all Public Shared declared functions.

    Another Question is, what is practically the difference between Private, Public and Public shared declared functions? As far I understand it from the help Private functions are only accesable from within the class where they are defined, Public are accessible from any class and Public Shared is accessable from any class but doesn't have access to local control elements in the class where it's defined. But I can't call Public declared function from another class even not when I generate an object of that type and try to address object.publicfunction. Only when it's public shared it appears as method but with the problem of not being able to use local controls. So practically I see no differences if a sub is declared private or public.

    Gunnar

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you are in the Mainform that is started with your app then you can reference it with 'Me'. You shouldn't need a second mainform, all the other forms should be created by the mainform and it can pass itself to them with Me.

    Private and Public you pretty much have right but Shared means it isn't tied to any instance so you don't need to make an object of the class and you can still use it. That is why those are the only ones you are seeing and why you had to use the actually Form class name.

    Here is an example:
    Last edited by Edneeis; Oct 17th, 2002 at 10:16 AM.

  14. #14

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Karlsruhe, Germany
    Posts
    8
    Ok, that makes sense. Thanks a lot for the example.

    Thanks,
    Gunnar

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