Problem with Me.close

Hi,

I'm just moving from VB6 and have a big problem with the Me.close() function.

Here is my example :

The default start form is a splash screen ; name it Splash.vb which as the same function than the Microsoft Word one, for example.
After a few seconds, I want to close it and load the main form, just name it Choices.vb .

The problem is, when I put the Me.close() function in the Splash.vb form, it just closes the program. Of course, I have put the Choices.show just before Me.close ; so I see briefly the main form, the the program closes.
To bypass this problem temporally, I changed the Me.close by Me.hide, but this is not a good solution of course.

Can anyone give me the solution, and the complete code of course This would really help me as I did not find a solution on the Web. I don't think that the 'Cancel' solution is good for me in that example...

Thanks a lot for your help everyone ;)
PascalLeBeta


Answer this question

Problem with Me.close

  • Paul Chapman MSFT

    Its a little hard to guess without see the code. However, have a look at my example that I posted on my blog, although its in C#, its pretty easier to get the idea of what is happening.

    http://davidkean.net/archive/2005/02/05/291.aspx

  • Tomsi

    Well, I think that all you need to know is in my thread. More simply, you can put a button on Form1. In its code, you put :
    Form2.show
    Me.close()

    What's happening is that your application closes after showing very briefly the Form2 .

    In fact, all what I say, is that when you close the starting form (and only the starting one), the entire program closes. When you close ANY of your other windows, it does not. And you will be agree with me that when the program closes after the splash screen, the program is not very interesting ;)

    Do you think it could be a bug in VB 2005 Beta 2

    Sorry but I did not find a solution in the link you provided.

    Thanks for your help.

  • Mark Gabarra - MSFT

    The problem you are facing is one of the learning curves you are faced with moving from VB6 to VB.Net. Since VB.Net follows true OOP principles the way you carry out the process you are trying to do has changed pretty dramatically.

    The reason Me.Close is closing your application is because Form1 is your default startup object. In VS 2003 you can change the startup object to Sub Main. I'm not sure if that functionality is present in VS 2005. Using Sub Main you can instantiate your forms and have more control, however another way to accomplish your goal would be something like this:

            Dim myForm As New Form2

            Me.Hide()
            myForm.ShowDialog()
            Me.Show()

    Then you can use Me.Close (or simply Close) to close your appliaction.

    HTH,
    Olice



  • Dave Bartolomeo - MSFT

    Glad you were able to find the option you were after in the new Project Designer! 

    As an FYI, we've heard the request to set a splash screen timeout a lot recently from customers, and we've recently added support for it in post-Beta 2 releases of VB.  In the meantime, you can use the Application Framework to set a splash screen manually.  The benefit of handling the splash screen timeout outside the main form is that you can perform startup logic while the timer is ticking.

    To enable this functionality...
    - Open the Application page of the Project Designer (Project->Properties)
    - Set the splash screen for the application (in the Application Framework group box)
    - Click the "View Application Events" button.
    - Paste in the following code (which is admittedly a little hairy scary):


    Imports System.Threading
    Namespace My

    'The following events are available for MyApplication
    '
    'Startup: Raised when the application starts, before the startup form is created.
    'Shutdown: Raised after all application forms are closed. This event is not raised if the application is terminating abnormally.
    'UnhandledException: Raised if the application encounters an unhandled exception.
    'StartupNextInstance: Raised when launching a single-instance application and the application is already active.
    'NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.

    Class MyApplication

    Private m_timeout As Long = 5000
    Private m_mainFormLoaded As Boolean
    Private m_timerThread As Thread

    ''' <summary>
    ''' The OnInitialize method is executed before the Startup event is fired
    ''' </summary>
    Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
       If (Not MyBase.OnInitialize(commandLineArgs)) Then
          Return False
       End If
       If (m_timerThread Is Nothing) Then
          m_timerThread = New Thread(AddressOf ElapseSplashTimeout)   
          m_timerThread.IsBackground =
    True
          m_timerThread.Start()
       End If
       Return True
    End Function

    ''' <summary>
    ''' Let the specified amount of time elapse
    ''' </summary>
    Private Sub ElapseSplashTimeout()
       Dim waitInterval As Long = 100
       Dim timeWaited As Long = 0
       
    While timeWaited < m_timeout
          Thread.Sleep(waitInterval)
          timeWaited += waitInterval
       End While
    End Sub

    ''' <summary>
    ''' OnRun() is called before the main form is shown
    ''' </summary>
    Protected Overrides Sub OnRun()
       
    If m_timerThread IsNot Nothing AndAlso m_timerThread.IsAlive Then
          
    m_timerThread.Join()
       
    End If
       MyBase.OnRun()
    End Sub

    End Class
    End
    Namespace


     

    Now any code you write in the Startup event will execute while the splash screen is shown, and the main form does need to have any specific splash screen logic.

    Hope this helps!

    Joe
    The VB Team


  • blanc0

    AArrggh!!

    I found where the problem was: it is a new option in the project properties : select Assembly, Enable framework application, and below you have the option to stop the program when the first form closes (by default) or when the last form closes.

    So, don't need to change any code, just check this option and all is fine!

  • Chris Breier

    I get a new problem :
    I need, in a form called Backup, to get back to the first loaded named Splash.
    After the form Backup is unloaded, the program stops BUT it does do what it should (load the Splash form) when I debug step by step (using the F11 key).
    I tried to load another form instead of Splash (Password for example) and it works (without step by step debugging).

    Is that a bug, or do you have a solution/workaround

    Thanks for your help

    Bonus question ;) : I need to set a variable which MUST be available even if the form it is made for is closed. I have tried a "Public MyVariable" but id does not work, instead of VB6. Thanks again.

  • Problem with Me.close