Results 1 to 2 of 2

Thread: Functions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017


    Hi. I would like to create a function called MainMenu. I have a system with many forms. Instead of rewriting the following lines for the main menu button :



    msgbox("You are back on the main menu")

    Is there a way of creating a global function which can be accessed by all the forms in the system so i just can call the function Mainmenu() which has these lines of code above.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010

    Re: Functions

    I'd have to be a little smart aleck to say "no", but you do have to make some small modifications in order to make it work.

    If you create a Module, every method in the Module will be callable from every other type in the application, so long as that Module's namespace is imported. I don't generally like using Modules, but you didn't ask if it was "good", you asked "Can I do it?"

    I would strongly recommend picking a different name than "MainMenu". In fact, it will be illegal to call your method "MainMenu", since you have a form named Mainmenu in the project. To VB, "MainMenu" and "Mainmenu" are the same name, and you can't name two different things with the same name. So if you call it "MainMenu()", you'd have to start using fully-qualified names, and the whole point is to not have to do that. Personally, I think "ShowMainMenu()" is a good name, because that's what you want it to do. Technically, this is a Sub, not a Function.

    So I'd write this:
    Module Navigation
        Public Sub ShowMainMenu(ByVal currentForm As Form)
            MessageBox.Show("You are now on the main menu.")
        End Sub
    End Module
    You would call it from some random form like:
    It's important for it to take that parameter, there's not another good way for a Module to know which form to hide.

    (Be careful, you are using a feature called 'Default Instances' and it usually ends up shooting the user in the foot.)
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

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