Results 1 to 8 of 8

Thread: Problem with Sub Main

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Amsterdam
    Posts
    74

    Problem with Sub Main

    I have a sub Main() as the start up for my project.

    VB Code:
    1. Module Module1
    2.     Public Sub Main()
    3.         Dim myForm As New FrmLogin()
    4.         'do something
    5.         myForm.Show()
    6.         myForm.Activate()
    7.     End Sub
    8. End Module

    It shows the FrmLogin, and then the application exits.
    Why it happens like this and how to make the application to run?

    I have place the typical login entry controls in the FrmLogin form, with no code written by me.

  2. #2
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    u need to show the form by using showdialogue method

    VB Code:
    1. Module Module1
    2.     Public Sub Main()
    3.         Dim myForm As New FrmLogin()
    4.         'do something
    5.         myForm.ShowDialogue()
    6.     End Sub
    7. End Module

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Amsterdam
    Posts
    74
    Thanks pvBangera, it solved the problem.
    So confusing, in VB we needed only to call the .Show() method....

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

    "Module Module1
    Public Sub Main()
    Dim myForm As New FrmLogin()
    'do something
    myForm.ShowDialogue()
    End Sub
    End Module"


    Not good. That way MyForm will have to stay open. A better way is to replace
    myForm.ShowDialog

    with

    Application.Run(myForm)
    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.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    There's also a scope issue. You can declare the form as a global variable in the module, then .show() in sub main will work.

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



    "There's also a scope issue. You can declare the form as a global variable in the module, then .show() in sub main will work."

    Not in my copy of VB.NET. Have you tried it?
    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.

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Originally posted by taxes
    Hi salvelinus



    "There's also a scope issue. You can declare the form as a global variable in the module, then .show() in sub main will work."

    Not in my copy of VB.NET. Have you tried it?
    Yes, the other day. I've got VB 2002. If I declared the form in Sub Main, then did Application.Run, the form would briefly flash into existance, then disappear. The app would still be listed as running, according to the IDE, but no forms were visible. My maybe faulty assumption is that it was a scope issue, because when I declared the form as global, it worked fine. It also allowed me to access/manipulate values on the form from other forms.
    If the event/function/procedure where the form is declared doesn't exit until the form is closed, then you're ok. But if you need the form after the e/f/p, you're out of luck.
    VB Code:
    1. private sub DoSub()
    2.    dim frmX as New frmOtherForm
    3.    dim i as integer
    4. 'yada yada
    5. Exit Sub
    i is out of scope once Exit Sub executes. Shouldn't it be the same for frmX?

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

    "Yes, the other day. I've got VB 2002. If I declared the form in Sub Main, then did Application.Run, the form would briefly flash into existance, then disappear. "

    Yup! That is why I said it does not work


    "The app would still be listed as running, according to the IDE, but no forms were visible. "

    No. The application ends and you are back in the Design environment.


    " My maybe faulty assumption is that it was a scope issue, because when I declared the form as global, it worked fine. "

    If you use Form.Show in the Sub Main, it does not matter whether you declare the Form public or otherwise. The result will still be that the form flashes and the programme ends. You either use Form.ShowDialog or Application.run(Form)


    "It also allowed me to access/manipulate values on the form from other forms."

    I can't understand this. The programme has finished and nothing is accessible. I am obviously misunderstanding you.


    "If the event/function/procedure where the form is declared doesn't exit until the form is closed, then you're ok. But if you need the form after the e/f/p, you're out of luck. "


    Understood, but you are not going to design the application to perform one routine.

    "i is out of scope once Exit Sub executes. Shouldn't it be the same for frmX?"

    Certainly. But we are talking about starting from a module Sub Main


    I think the best practice is to use the Application.Run method in a Sub Main. This then gives you total freedom.


    Can any of you guys please say if I am wrong and also tell me why when the form which starts the application, whether from a module Sub Main or not, is closed, all other forms close - unless those other forms were opened with a ShowDialog method in the Sub Main?

    Regards,
    Last edited by taxes; Jan 31st, 2004 at 04:21 PM.
    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