Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Running code when a form is unhidden

Running code when a form is unhidden

Running code when a form is unhidden

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.

RE: Running code when a form is unhidden

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.



RE: Running code when a form is unhidden

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

RE: Running code when a form is unhidden

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.

RE: Running code when a form is unhidden

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

RE: Running code when a form is unhidden

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.

RE: Running code when a form is unhidden

Instead of Form_Load use Form_Activate, that way everytime the form gets priority Form_Activate will run.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close