Results 1 to 12 of 12

Thread: Duplicate form by button click

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Exclamation Duplicate form by button click

    Hey all,

    I want to create an application which can reset itself quickly and easily (without a function to do so). I thought one way to do this is to duplicate the form in it's original form (variables, etc) from the point it first loaded.

    This is how I thought it could be done:
    1. Click button to restart application.
    2. Form in use is duplicated by creating a new form with Dim newForm as new Form.
    3. New form contains all properties of the original form at the time it was first created.
    4. Original form is removed, and new form is used; thereby 'restarting' the application.

    I can't find any simple way to do this without making a function which completely resets every variable and object (there's a fair number of them).

    Any ideas/suggestions?

    -MoK

    PS: I'm using Visual Basic Express Edition 2008.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Duplicate form by button click

    If you want to restart an application then you can simply call Application.Restart. If you want to reset the state of an existing form then that's what you should do. Creating a new form is going to take more time that resetting the state of the existing form. If you're trying to save yourself a few minutes of development time by implementing a hack instead of doing something properly, I suggest accepting the fact that as developers we have to write as much code as is appropriate to the situation. If there are lots of properties to reset then you write more code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Re: Duplicate form by button click

    Application.Restart() gives me an error...

    Win32Exception was unhandled.
    Unknown error (0xffffffff)
    -MoK

    PS: I've been coding for two years. I know about coding.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Duplicate form by button click

    There's no way that we can diagnose that error with just that information. If you know about coding then you most likely also know about debugging.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Re: Duplicate form by button click

    How can I explain an error that tells me nothing? I simply click on a button and it runs Application.Restart, and the error comes up. I can't explain it anymore.

    -MoK

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Duplicate form by button click

    If an exception is thrown and not handled then, generally speaking, the Exception Assistant window is displayed. That window provides access to the same information that you can access in code via the Exception object if you had handled the exception in code. In this case, given that it says "Unknown error", it might not provide a lot of information and diagnosis may be difficult. You're the one with the code in the debugger though, so you have a chance of doing so, where we have no chance with no more information that the fact that you called Application.Restart and an unknown Win32Exception was thrown.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    New Member
    Join Date
    Sep 2009
    Posts
    14

    Re: Duplicate form by button click

    enclose the offending code in a TRY / CATCH block, and check the stack trace

    see if you can pinpoint where exactly the error is occurring.



    i just worked up a sample app that did what you wanted.

    - it has a few labels, textboxes, etc that have default values
    - a button to restart with this code:
    Code:
    dim frm as new Form1 '' this is the startup form
    frm.show()
    me.close()
    most importantly: you have to go into the "My Project" --> "Application" tab and change the setting "Shutdown Mode" to "When Last form closes" (by default its "When Startup form closes"

    doing that it creates a new form with the default values set - in essence starting over.

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Duplicate form by button click

    Code:
    dim frmMainNew as New frmMain      ' create new instance of frmMain Class
    frmMainNew.show()                       ' show new frmMain Class instance as frmMainNew
    me.close()                                   ' close frmMain
    just an idea, untested, never needed to do this... i may be missing a step?

    edit:
    sry Roccoman, i said the same thing as you.
    Last edited by MattBodin; Aug 25th, 2010 at 05:27 AM. Reason: missing code block
    As my father would say, "It doesn't cost much to pay attention."

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Duplicate form by button click

    Quote Originally Posted by MattBodin View Post
    Code:
    dim frmMainNew as New frmMain      ' create new instance of frmMain Class
    frmMainNew.show()                       " show new frmMain Class instance as frmMainNew
    me.close()                                   ' close frmMain
    just an idea, untested, never needed to do this... i may be missing a step?
    Only 4 1/2 hours after Roccoman suggested the same thing. I still say that that is a hack though. There's really no reason that you can't use the form you've already got. It seems the only issue is that writing a few more lines of code to reset the existing variables, etc, is too much trouble.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Duplicate form by button click

    i never siad i was sure, or correct in my post. it was a suggestion, which seems to do what he wanted, im a noob here and my ears(eyes) are wide open.

    for the record, i agree, ide use the same form. thats y ive never tried to do it before. unless there is a reason why you can't MoKs?
    As my father would say, "It doesn't cost much to pay attention."

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Re: Duplicate form by button click

    Never mind, Application.Restart() does work. It was the debugger on the computer I was using.

    I still say that that is a hack though.
    What is?

    It seems the only issue is that writing a few more lines of code to reset the existing variables, etc, is too much trouble.
    The current reset function is 139 lines long, all of which is required.

    There's really no reason that you can't use the form you've already got.
    I was trying to, but I couldn't find anything.

    -MoK

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Duplicate form by button click

    Quote Originally Posted by MasterOfKings View Post
    What is?
    I said:
    I still say that that is a hack though. There's really no reason that you can't use the form you've already got.
    Is it not obvious that I meant that NOT using the form you've already got, i.e. discarding a perfectly good form and creating a new one, is the hack?
    Quote Originally Posted by MasterOfKings View Post
    The current reset function is 139 lines long, all of which is required.
    Then so be it.
    Quote Originally Posted by MasterOfKings View Post
    I was trying to, but I couldn't find anything.
    Apparently you already had something. There's no magic bullet that will reset everything. If you want something set to a specific value then you have to set it. Controls may have methods like Clear and ResetText that can help but you still have to attend to each one individually.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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