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

查看:167
本文介绍了为什么在编译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

了解控制生命周期和主要事件

以下是一个代码段


编译项目

Compiling the Project

当项目编译成
应用程序或组件时,Visual Basic
所有的表单文件不可见,
一个接一个,以便将
的信息写入
编译文件。控制实例获取
的Initialize,ReadProperties和
WriteProperties事件。控件的
属性设置被编译为
完成的可执行文件。

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