Running code when a form is unhidden
Running code when a form is unhidden
(OP)
I have a hangman program which involves 2 forms. The first form sets the options (such as the word to guess). These variables are public and are used for the 2nd form, where the guessing of letters takes place. Code must be executed on the 2nd form before the guessing can commence (i.e. printing the ? characters for each letter of the word, etc). This is fine the first time the program is run as the code can be put into the Form_Load procedure. However, when the word is guessed, I need it to go back to the form with options, where the word is changed (achieved by hiding and showing the forms). Then going back to the 2nd form, the code in Form_Load is not run. I need this code to run each time the program runs. I need to find out how to either:
- Close a form so the Form_Load procedure can be rerun; or
- Find a way of running code on a form when it is loaded in a different way, such as Show.Form
Any help is appreciated.
- Close a form so the Form_Load procedure can be rerun; or
- Find a way of running code on a form when it is loaded in a different way, such as Show.Form
Any help is appreciated.
RE: Running code when a form is unhidden
1) When you close the second form(where the guessing takes place) unload it with the
Unload Form2
command. That way when the form is opened the next time, it will have to go through the Form2_Load sub again.
2) You can initialize the Form2_Load event at anytime, even when the form is already loaded. In another sub, just call the Form2_load sub. That is
Private Sub SomeAction()
Call Form2_Load
End Sub
This will execute ALL the code in the forms load event so be carefull whats in there.
Hope this was helpful, just post back here if you need more of an explination.
Cheers,
hojo
RE: Running code when a form is unhidden
I am not electrical in any way but dont know how to change it
RE: Running code when a form is unhidden
If you want to change the electrical tag, you should be able to do so with the 'personal profile' link near the top left of the page.
RE: Running code when a form is unhidden
I went to personal profile and couldn't find a way of changing it. That's why I resorted to making the comment as my signature.
I am not electrical in any way but dont know how to change it
RE: Running code when a form is unhidden
Either unloading/reloading the form or using the form.activate event would work for your application; using form.activate would be 'better' in the sense that it takes less cpu time than unloading/reloading. If you have a simple form and a decent computer setup you probably won't notice any difference between the two methods, but as the form complexity increases (more controls, more code in the load and unload events, etc) it will be noticeably longer to unload/reload.
RE: Running code when a form is unhidden