|
-
Aug 24th, 2010, 09:02 PM
#1
Thread Starter
New Member
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.
-
Aug 24th, 2010, 09:09 PM
#2
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.
-
Aug 24th, 2010, 09:23 PM
#3
Thread Starter
New Member
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.
-
Aug 24th, 2010, 09:34 PM
#4
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.
-
Aug 24th, 2010, 09:36 PM
#5
Thread Starter
New Member
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
-
Aug 24th, 2010, 09:54 PM
#6
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.
-
Aug 25th, 2010, 12:48 AM
#7
New Member
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.
-
Aug 25th, 2010, 05:22 AM
#8
Junior Member
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."
-
Aug 25th, 2010, 05:29 AM
#9
Re: Duplicate form by button click
 Originally Posted by MattBodin
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.
-
Aug 25th, 2010, 06:08 AM
#10
Junior Member
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."
-
Aug 25th, 2010, 07:08 PM
#11
Thread Starter
New Member
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
-
Aug 25th, 2010, 07:32 PM
#12
Re: Duplicate form by button click
 Originally Posted by MasterOfKings
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?
 Originally Posted by MasterOfKings
The current reset function is 139 lines long, all of which is required.
Then so be it.
 Originally Posted by MasterOfKings
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|