CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2005
    Posts
    20

    Unhappy Form Disappear Automatically

    This is very strange, I have a form, a very simple "Hello World" type of form. If I just do F5, the form shows up, no problem. Now I added a module, with the following code:
    ***********************
    Module MainModule
    Dim frm1 As New frmLogin1()

    Sub Main()
    If PrevInstance() Then
    msg = "This Program is already running. Please shut down the currently running"
    msg = msg & " process before starting a new one"
    MsgBox(msg, vbInformation)
    Exit Sub
    Else
    Startapplication()
    End If
    End Sub

    Function PrevInstance() As Boolean
    If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
    Return True
    Else
    Return False
    End If
    End Function

    Sub Startapplication()
    frm1.Show()
    End Sub
    End Module
    **********************************

    And I changed the startup object to "sub main" in the project property.

    Now, when I do F5, the frmLogin shows, and then disappear automatically...

    Can anybody tell me what I did wrong? many thanks...
    Last edited by MacArthur; August 8th, 2005 at 04:59 PM.

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Form Disappear Automatically

    If you use a Main method then the application exits when the Main method completes. If you call Show on a form, as soon as the form is displayed the method returns. This means that the Main method then completes and your app exits, hence the form closes. That is why you call Application.Run with an instance of your form class rather than Show, e.g.:
    Code:
    Application.Run(frm1)
    Application.Run does not return until the form it was passed is closed.

  3. #3
    Join Date
    Apr 2005
    Posts
    20

    Re: Form Disappear Automatically

    Thank you very much...

  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Form Disappear Automatically

    that or showdialog instead
    Nicolas Bohemier

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Form Disappear Automatically

    ShowDialog is for showing a dialogue. Application.Run is for running an application. While ShowDialog may appear to do the same thing it is more correct to call Application.Run in this case.

  6. #6
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Form Disappear Automatically

    The point here is to stop the thread both do it. But in any case, do application.run..
    Nicolas Bohemier

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

Featured