dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 20

Thread: ReInitialize program

Hybrid View

  1. #1
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125

    ReInitialize program

    Hello,

    I would like to create a 'new' button that sets everything on the form ( and all vars) back to 'nothing'.

    ... = "" is a way to do this ( very unpractical I think )

    but is there a way to 'restart' the program ? I could create an exe wich hosts the main program. But I would prefer 1 exe because it is a school project.

    Is there any simple way to 'restart' the app ?

    Much Thanks!
    Bert Willekens,

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: ReInitialize program

    Maybe unpractical, but AFAIK the normal procedure is to just reset every variable. That may be due to the fact that applications tend to contain more than on form/dialog, and when the user wan't to 'reset' something he only wan't to reset the current form/dialog, not the whole application.

    Anyway, a way to restart the app is to execute a second instance of the application somwhat right before you terminate the first instance.

    You can use Process class to execute applications.

    - petter

  3. #3
    Join Date
    Jan 2006
    Posts
    293

    Re: ReInitialize program

    How much stuff do you have to "reset"? Just a matter of calling the code you need in one sub, resetting what you want. Usually, if you use the correct scope of your variables, then it doesn't require much to "reset" the variables to what they were when the application started. The only ones that would need it would be your class level variables, and then clearing the controls of whatever they contain or resetting the text of the controls to what it was before.

    What I guess I am saying is we would need more info as to what your project contains and what all you need to reset in order to give you a more definite answer.

  4. #4
    Join Date
    Jan 2006
    Posts
    293

    Re: ReInitialize program

    In 2005, there are options you can do in order to re-initialize your form, to an extent. It requires a couple of things.

    1. In your project properties, change the "ShutDown Mode" (listed under Application Tab) to "When Last Form Closes", instead of the default "When Startup Form Closes"

    2. Create a module that has a public sub to open a new instance of Form1, and then close the calling form. Sample code that worked inside the module:
    Code:
    Public Sub RestartMainForm(ByVal CallingForm As Form1)
            Dim MyNewForm As New Form1
            MyNewForm.Show()
            CallingForm.Close()
    End Sub
    3. From your main form (assumed to be a "Form1" type in this case), call the public sub when you wish to "restart" the form. An Example using the code from above:
    Code:
    RestartMainForm(Me)
    Calling it should open a new Form1 and show it, and then close the form where you called RestartMainForm(). Since the application will close only when the last form is closed, it will not exit, and you get a new fresh Form1.

    **Note - this essentially "resets" your Form1 only. Any Module variables or variables in any other classes will not be reset.

    In 2003, you can do something similar starting your program using Sub Main()...
    Last edited by gigemboy; October 28th, 2006 at 07:39 PM.

  5. #5
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125

    Re: ReInitialize program

    Thank you Gigemboy, works like a charm!

    Just what I needed
    Bert Willekens,

  6. #6
    Join Date
    Oct 2006
    Posts
    449

    Re: ReInitialize program

    Am bit confused... I have main form as Form1 and from here I call Form2.

    Should I have put the following code in 'Form2'...

    Public Sub RestartMainForm(ByVal CallingForm As Form1)
    Dim MyNewForm As New Form1
    MyNewForm.Show()
    CallingForm.Close()
    End Sub

    And this following code in 'Form1'

    Am I right? PLease guide me. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)