Event excitation sequence in Windows form

Source: Internet
Author: User

A few days ago, when I saw Mr. Zhou's Thought of software engineering practitioners (http://www.doany.net/), I saw the sequence of Windows events and the impact on other processes, today, we have encountered a similar problem.

A system written these days may occasionally encounter unexpected events at night: it is strange that the results returned after a database query is executed. InitialCodeIt is VB6, and the process is very simple. A form has a query method, which is roughly the following code:

Public sub refreshdb (strsql as string)
'Set of record results
Set c = new collection
Dbquery:
'Query code
...
End sub

When querying the method, write part of the result to set C for other operations in the future, and fill in the query result to a grid interface. This method is also called in the load method of the form to obtain the default result.

In VB6, if the form is not initialized, the call sequence is:
(1) execute the refreshdb code before "dbquery;
(2) Call the form load event;
(3) After the code in the load event is executed, return the code after "dbquery.

Therefore, because the set C does not have a new one, the execution is interrupted when refreshdb is called, the load event is called (refreshdb is called), and then the result is returned, resulting in inconsistent results.

According to the idea, either execute the load event first, or execute the refreshdb method first, and then call the load event (in VB.net 2005 ),However, in VB 6, the load event is called when the form is displayed or a variable in the form is initialized for the first time.In the above Code, the load event is executed because the collection Initialization is called. The load event of VB 6 has some meanings of the constructor, while VB.net is completely object-oriented, new is the constructor, and load is an event before the display of the form. For VB 6ProgramPersonnel, these differences should be noted. In turn, for this problem, if it is in the order of. net, it is easy to find the problem :).

The correct method is to initialize the form first, for example, call the show or activate (. NET and VB are show and setfoucs) methods, then call refreshdb, or initialize the form after it is started. Therefore, in development, the sequence of events in the form must be clear; otherwise, unnecessary troubles may occur. Second, I personally think that unless necessary, for example, for project requirements or platform restrictions (including hardware), VB 6 programmers abandoned the old platform for a long time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.