Hi everyone,
I have a question about Visual Basics.........

When do the following events occur with a form object: Load, Unload, Activate, Deactivate, GotFocus, LostFocus?

Recommended Answers

All 4 Replies

Almost sounds like you are needing answers to a homework assignment, but since I give every one the benefit of the doubt, here are the answers to those questions:

A static variable allows the variable to maintain it's previous value through repeated raises of an event. Meaning that, if you have a variable say "X" and you have a command button. If you add the code snippit to the command button:

x = x + 1
msgbox x

If the variable X is NOT Static, you should always get the same response (which in this case would be one), because every time the event finishes (every time the code in the button is done running) it destroys all the local variables. So, Every time you click the button, the variable gets re-created, as nothing, and then has 1 added to nothing. It doesn't take a math teacher to know that 1 plus nothing is 1. So, it will always be one, because it's always erased when the code is done.

However, Should the variable be "static" then it will keep it's value until the program terminates, or the value is over-written by the programmer. If, in the code above, the variable X where to be static, then it would constantly add 1 to X's previous value. So, the first time you click the button, it would show 1, the second time, 2, and so forth and so on. Basically, static allows the variable to keep it's value while the program runs.

The difference between passing variables byref and byval, is the ability for the sub or function to manipulate variables. If you pass a variable byval (by it's value), then you are making a xerox copy of the variable, and sending that information to the function. So, even if the function decides to alter the information I have sent to it, the variable that I passed to it, will never be able to be changed. Because the truth is, I never passed it a variable (if it's byval), I only passed it the VALUE of the variable.... not the variable, I only gave it a copy, not the original.

Passing variables byref (by their reference) allows the function to alter the contains of the variable. This all has to do with "scope", and is a really good practice. If I write a function that I want to be able to change the variable in question, so that I don't have to make that change in the main code, I can pass it by reference. Byref is the direct opposite of byval. One is passing a copy of the information to the function, and the other is actually sending it the original data. Understanding byref and byval makes more sense in a language more in depth than VB, such as C or Perl, where pointers and other factors can play a role in programming scope, but for the sake of simple understanding, byval is giving the function a copy of the data, and byref is giving it the actual original data. If changes the copy, no big deal. The original is still in tact. If it's byref, and it changes the data, the data is changed for good.

Hi,

For the even order I found this in MS Access help but I am fairly sure the order is the same in VB.
When opening a form: Open => Load => Resize => Activate
When closing a form: Unload => Deactivate => Close

And quoting the help file:
"When you switch between two open forms, the Deactivate event occurs for the first form, and the Activate event occurs for the second form. If the forms contain no visible, enabled controls, the LostFocus event occurs for the first form before the Deactivate event, and the GotFocus event occurs for the second form after the Activate event."

Hope this helps

Yomet

BTW Comatose - I think you might have posted that very involved reply in the wrong thread, could I be correct? ;)

Hmn,

Strangely enough, the questions that I answered actually were posted! It looks as if he must have edited the thread (or someone else did), and changed the original post :eek: . Strange.

That was kind of what I suspected but I wasn't sure. Howcome someone can modify a posting after a response has been given? I think that should be blocked.

Have fun

Yomet

P.S. Student2005, or whoever modified the original posting - please do NOT modify a posting that has a reply, instead just add a reply to the end of the thread. That way everyone can follow what actually happened.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.