为什么在编译 VB6 应用程序期间触发事件? [英] Why is an event firing during compilation of a VB6 app?

查看:21
本文介绍了为什么在编译 VB6 应用程序期间触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译 VB6 应用程序,但它失败并出现错误运行时错误 '91':对象变量或未设置块变量".事实证明,用户控件的 Resize 事件在编译期间触发,并调用试图访问尚未实例化的对象的代码.

I am trying to compile a VB6 application, but it fails with the error, "Run-time error '91': Object variable or With block variable not set". It turns out the Resize event of a user control is firing during compilation and calling code that attempts to access an object that has not been instantiated yet.

为什么在编译过程中会触发事件,有什么方法可以阻止它?

Why is an event firing during compilation and is there any way to stop it?

编辑:我这里有一些代码,但不相关.问题是由于 UserControl 代码(即 Initialize、ReadProperties、Resize 和 WriteProperties 事件)可以在意外时间执行.如果这些事件中的代码依赖于其他代码来初始化其任何数据结构,那么它很有可能会失败,因为该初始化代码可能尚未执行.尤其是在编译期间,什么都不应该执行!我认为这是一个错误,但我确信微软可以通过某种方式对其进行合理化.

Edit: I had some code here, but it's not relevant. The problem results from the fact that UserControl code (namely the Initialize, ReadProperties, Resize, and WriteProperties events) can execute at unexpected times. If the code in these events relies on other code to initialize any of its data structures, there's a good chance it's going to fail because that initialization code may not have executed. Especially during compilation when nothing is supposed to be executing! I'd call this a bug, but I'm sure Microsoft can rationalize it somehow.

推荐答案

这是一篇关于用户控件事件生命周期的好文章

Here's is a good article on the lifecyle of user control events

了解控件生命周期和关键事件

这是一个片段

编译项目

当项目被编译成应用程序或组件,Visual Basic不可见地加载所有表单文件,一个接一个,为了写它们包含的信息编译的文件.一个控制实例得到Initialize、ReadProperties 和写属性事件.控制的属性设置被编译成完成的可执行文件.

When the project is compiled into an application or component, Visual Basic loads all the form files invisibly, one after another, in order to write the information they contain into the compiled file. A control instance gets the Initialize, ReadProperties, and WriteProperties events. The control's property settings are compiled into the finished executable.

它没有提到调整大小(在运行时或在设计时物理调整容器上的用户控件大小时发生).也许您的 Initialize 事件正在调整用户控件的大小?

It doesn't mention resize (which happens during run-time or when you physically resize the usercontrol on a container in design-time). Maybe your Initialize event is resizing the user control?

为避免错误,您可以在执行任何操作之前检查是否已创建违规对象:

To avoid the error you can check if the offending object has been created before doing anything:

If Not Object Is Nothing then
  do something

这篇关于为什么在编译 VB6 应用程序期间触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆