Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running code when a form is unhidden

Status
Not open for further replies.

ybbun

Electrical
Jul 1, 2003
3
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.
 
Replies continue below

Recommended for you

From what i think your trying to do i would sugest 2 things.

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
 
2) does not work as I can't find a way of calling from the current form, without pressing a button each time. However, 1) does work. I was trying to use Form2.Unload (based on the same principles as Form2.Show etc) instead of Unload Form2. Thanks, it works brilliantly now :)

I am not electrical in any way but dont know how to change it
 
You could also try using the form's activate event. For specifics, refer to the help files. This might be an easier way to go.

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.
 
What is the form's activate event? Would this be better than unloading the form each time?

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
 
The form's activate event is similar to the 'Gotfocus' event for a control. The help files will explain it better than I can in this short post.

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.
 
Instead of Form_Load use Form_Activate, that way everytime the form gets priority Form_Activate will run.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor