Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Call a form’s sub from a Sub Main()

Call a form’s sub from a Sub Main()

Scheduled Pinned Locked Moved Visual Basic
help
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gregcost
    wrote on last edited by
    #1

    I start my project with a module called SubmainController.vb which contains a Public Sub Main() In this public sub main I want to call a sub from another form in the project. I’ve tried the following without ant success. Dim Myform1 as new Form1 Call Myform1.LoadFromFile (property access my assign to the property or use its value Dim ProjectName as string “Web Project” Call ProjectName.Myform1.LoadFromFile (myform1 not a member of string) I though this would be simple. I appreciate help Thanks GregC Thanks, GregC

    J 1 Reply Last reply
    0
    • G gregcost

      I start my project with a module called SubmainController.vb which contains a Public Sub Main() In this public sub main I want to call a sub from another form in the project. I’ve tried the following without ant success. Dim Myform1 as new Form1 Call Myform1.LoadFromFile (property access my assign to the property or use its value Dim ProjectName as string “Web Project” Call ProjectName.Myform1.LoadFromFile (myform1 not a member of string) I though this would be simple. I appreciate help Thanks GregC Thanks, GregC

      J Offline
      J Offline
      John Kuhn
      wrote on last edited by
      #2

      How about making the Sub a Public method of a separate class, thus making it available to both forms without the need of instantiating either form unneccessarily. As an alternative, if the method is declared Shared, it should be accessible even without creating an instance of the form, simply by calling Form.MethodName(). Oh, and you don't need to use "Call" anymore.

      G 1 Reply Last reply
      0
      • J John Kuhn

        How about making the Sub a Public method of a separate class, thus making it available to both forms without the need of instantiating either form unneccessarily. As an alternative, if the method is declared Shared, it should be accessible even without creating an instance of the form, simply by calling Form.MethodName(). Oh, and you don't need to use "Call" anymore.

        G Offline
        G Offline
        gregcost
        wrote on last edited by
        #3

        Hi should have added that this is an event. Public Sub loadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadFromFile.Click is it possible to raise an event on another form from a sub main? i'm not quite sure how to code that. Would i need to change the above click event code and how would I call or raise the event from the sub main? And would rasing the event execute the click event? Thanks, GregC

        J 1 Reply Last reply
        0
        • G gregcost

          Hi should have added that this is an event. Public Sub loadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadFromFile.Click is it possible to raise an event on another form from a sub main? i'm not quite sure how to code that. Would i need to change the above click event code and how would I call or raise the event from the sub main? And would rasing the event execute the click event? Thanks, GregC

          J Offline
          J Offline
          John Kuhn
          wrote on last edited by
          #4

          You can certainly do something like what you describe; here's what I did:

          Module MainModule
          Sub Main()
          FormRaisingEvent.LoadFromFile(New Object, New EventArgs)
          End Sub
          End Module

          and the form's code looks like this:

          Public Class FormRaisingEvent
          Inherits System.Windows.Forms.Form

              ' Windows Form Details omitted...
          
              Public Shared Sub LoadFromFile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                  MessageBox.Show("Hello!")
              End Sub
          
          End Class
          

          The important things are 1) declare the Sub as Shared and 2) when you call the method of the form from somewhere else, pass it the parameters required in the method's signature. Let me know if this answers your question.

          G 1 Reply Last reply
          0
          • J John Kuhn

            You can certainly do something like what you describe; here's what I did:

            Module MainModule
            Sub Main()
            FormRaisingEvent.LoadFromFile(New Object, New EventArgs)
            End Sub
            End Module

            and the form's code looks like this:

            Public Class FormRaisingEvent
            Inherits System.Windows.Forms.Form

                ' Windows Form Details omitted...
            
                Public Shared Sub LoadFromFile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                    MessageBox.Show("Hello!")
                End Sub
            
            End Class
            

            The important things are 1) declare the Sub as Shared and 2) when you call the method of the form from somewhere else, pass it the parameters required in the method's signature. Let me know if this answers your question.

            G Offline
            G Offline
            gregcost
            wrote on last edited by
            #5

            Hi John, THANKS! It worked GREAT! :) Thanks, GregC

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups