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

查看:17
本文介绍了为什么在编译 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 事件)可以在意外时间执行这一事实造成的.如果这些事件中的代码依赖于其他代码来初始化其任何数据结构,那么它很有可能会失败,因为该初始化代码可能尚未执行.尤其是在编译期间,什么都不应该执行!我将其称为错误,但我相信 Microsoft 可以以某种方式将其合理化.

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天全站免登陆