none
Call a Sub from another Form. RRS feed

Answers

  • Hi

    How can I Call a Subroutine on one Form from another Form.

    Hi

    First of all, the Sub needs to be Public. For example, on Form1,

    Public Sub MySub(s as String)

    - called from Form2,

    Form1.MySub("Freddy")


    Regards Les, Livingston, Scotland

    Thursday, June 22, 2017 10:34 PM
  • You can, it is not any problem. However, despite I'm always tell here that there is no bad practise; this is bad practise. 

    A Class should be meant to do what it is made for. A window Forms class is made for presentation. 

    Therefore it is in my way of splitting presentation and business logic, better to create a separate class and let both forms use that. 

    Be aware, a class is just a class, and a form is also just a class. 

    But if you call a form class to do something which is meant to do something simple, it is like sending the whole American Army to New York to get the dog of Trump into the White house. 

    For those who think: "I wrote this in fact as well in other words", yes it is true, so please don't mark this as answer. 


    Success
    Cor


    Thursday, June 22, 2017 11:06 PM

All replies

  • Hi

    How can I Call a Subroutine on one Form from another Form.

    Hi

    First of all, the Sub needs to be Public. For example, on Form1,

    Public Sub MySub(s as String)

    - called from Form2,

    Form1.MySub("Freddy")


    Regards Les, Livingston, Scotland

    Thursday, June 22, 2017 10:34 PM
  • Hi

    How can I Call a Subroutine on one Form from another Form.


    You're better off to create a class so that it's in scope to all of the forms. Then it's not an issue at all.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, June 22, 2017 10:38 PM
  • How can I Call a Subroutine on one Form from another Form.

    You need a variable that refers to that form.   How you do that depends on the relationship between the two forms - which was created first, was one created from code in the other, etc.  See
    https://vbdotnetblog.wordpress.com/forms/

    An alternative is to move the subroutine to a module. See
    https://msdn.microsoft.com/en-us/library/7825002w(v=vs.90).aspx

    Thursday, June 22, 2017 10:39 PM

  • First of all, the Sub needs to be Public. For example, on Form1,

    Public Sub MySub(s as String)


    Hi Les,

    I guess you meant [at least] "internal" (aka Friend in VB) as long as both forms are in the same assembly...

    Depending on what should be done, its better to use an instance of a class that covers "that", or, if its a display-relevant procedure, define that method in the form or [User-] Control indeed.

    Regards,

      Thorsten



    Thursday, June 22, 2017 11:02 PM
  • You can, it is not any problem. However, despite I'm always tell here that there is no bad practise; this is bad practise. 

    A Class should be meant to do what it is made for. A window Forms class is made for presentation. 

    Therefore it is in my way of splitting presentation and business logic, better to create a separate class and let both forms use that. 

    Be aware, a class is just a class, and a form is also just a class. 

    But if you call a form class to do something which is meant to do something simple, it is like sending the whole American Army to New York to get the dog of Trump into the White house. 

    For those who think: "I wrote this in fact as well in other words", yes it is true, so please don't mark this as answer. 


    Success
    Cor


    Thursday, June 22, 2017 11:06 PM