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

Thread: ReInitialize program

  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!

  7. #7
    Join Date
    Jan 2006
    Posts
    293

    Re: ReInitialize program

    The code you posted goes in a module. Then you can call the function anywhere you want. If you wish to re-initialize form1, then that code will suffice, passing in the reference to your form1. If you have another object, like form2 to re-initialize, then you would need to change the function parameter to accept a Form2 object and not a Form1 object (as well as changing the New Form1 object line to a Form2 object), and then just pass a reference to your form2 to the function.

    In my posted example above, the "RestartMainForm(Me)" was called from the same form (form1) that you want to re-initialize, because the "Me" was a reference to the form1 that the code was called from. It was just an example, and doesn't have to be that way.
    Last edited by gigemboy; November 6th, 2006 at 09:53 PM.

  8. #8
    Join Date
    Oct 2006
    Posts
    449

    Re: ReInitialize program

    Hi gigemboy: Thank you for your detailed post. But I have a query. I have 'Form1' which is the main startup form and another is 'Form2'. I have the code 'RestartMainForm(Form1)' in my Form2 but what happens is it opens a another instance of 'Form1'. So everytime I close the 'Form2' another instance of 'Form1' is being opened. How do I control this? I want only one instance of 'Form1' at any point of time. Please guide me. Thank you.

  9. #9
    Join Date
    Jan 2006
    Posts
    293

    Re: ReInitialize program

    Well you arent passing the right form reference. I stated that "Me" was used on Form1. Me is a reference you can use on the form you place the code on to have a reference to that same form you are running the code on. So you are passing a reference to Form2 when you use "Me" on Form2. You have to call RestartMainForm passing a reference to your opened Form1.

    If you are wanting to restart Form2 and not Form1 (I am not sure from your post), then you have to change the function in the module (as stated above) to handle your Form2 object, not the Form1 object
    Last edited by gigemboy; November 7th, 2006 at 10:44 AM.

  10. #10
    Join Date
    Oct 2006
    Posts
    449

    Re: ReInitialize program

    No gigemboy, am not using 'Me'. I use 'RestartMainForm(Form1) in my 'Form2' to reinitialize 'Form1'. Where am I going wrong? What happens is everytime the 'Form2' closes a new instance of 'Form1' is opening.
    Please guide me. Thank you.

  11. #11
    Join Date
    Jan 2006
    Posts
    293

    Re: ReInitialize program

    Then where did you place the call to RestartMainForm in your Form2? In the closing event? Do you have any other code that you placed there before that is creating a new Form1 instance?

  12. #12
    Join Date
    Oct 2006
    Posts
    449

    Re: ReInitialize program

    Yes, I have lot of codes and events in Form2.

    I have Form2_Load event, followed by Combobox_SelectedIndexChanged event, followed by Button_Click event where-in I have the 'RestartMainForm (Form1)' code placed as mentioned below:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    MessageBox.Show("Data updated!!", "My Application")
    RestartMainForm(Form1)

    End Sub

    WHERE AM I GOING WRONG, gigemboy! PLease guide me through! Thank you.

  13. #13
    Join Date
    Jan 2006
    Posts
    293

    Re: ReInitialize program

    Well that code is fine. You mentioned how it is creating a new instance when you close form2, so how are you closing form2? Are you just closing it manually or through code? Do a search for "RestartMainForm" and see if you have it somewhere else in your form that you don't realize. If you don't, then are you perhaps calling a button1.performclick method elsewhere on your form?

  14. #14
    Join Date
    Oct 2006
    Posts
    449

    Re: ReInitialize program

    gigemboy... Earlier I was having a code 'me.close()' placed in my Button1_Click event as mentioned below. But then I removed after inserting the code 'RestartMainForm(Form1)' since this code 'RestartMainForm(Form1)' closes the calling form.

    ----------------------------------------------------------------------------------------
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    MessageBox.Show("Data updated!!", "My Application")
    ' ..................... me.close()'
    RestartMainForm(Form1)

    End Sub
    ----------------------------------------------------------------------------------------

    I did the search and I don't have the code 'RestartMainForm(Form1)' anywhere else in the Form2. But I have a another 'Button2_Click' event and the code is as follows, wherein I click the button to close, which I don't use it either way since if the user performs a action, he gets to see the messagebox 'Data updated' and then we do call 'RestartMainForm(Form1)' which anyway closes the 'Form2'.

    ----------------------------------------------------------------------------------------
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
    End Sub
    ----------------------------------------------------------------------------------------

    Am sorry for taking much of your time in this process and I really appreciate your patience shown towards me. Thank you.

  15. #15
    Join Date
    Oct 2006
    Posts
    449

    Re: ReInitialize program

    Gigemboy... am waiting for your reply from you for my last post. Please scroll down. Am still not getting it right. Please guide me. Thank you.

Page 1 of 2 12 LastLast

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)