vba程序的vb6封装_调试VBA和VB6应用程序

vba程序的vb6封装

The debugging module can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.

可以通过“调试”菜单项访问调试模块。 该菜单项通常可以在IDE的主菜单行中找到,如下图所示。

There is also a companion Debug Toolbar that looks like the following and that toolbar will be the focus of this tutorial:

还有一个配套的调试工具栏,如下所示,该工具栏将成为本教程的重点:

If you can't find that toolbar then you can add it to the menus by pulling down the View menu item, clicking Toolbars, and checking Debug.

如果找不到该工具栏,则可以通过下拉“视图”菜单项,单击“工具栏”,然后选中“调试”将其添加到菜单中。

Components of the Debug Toolbar

调试工具栏的组件

They are (in order)

他们是(按顺序)

o Continue (toggle)

o继续(切换)

o Break

o休息

o End

o结束

o Toggle Breakpoint

o切换断点

o Step Into

o进入

o Step Over

o越过

o Step Out

o走出

o Locals Window

o当地窗口

o Immediate Window

o立即窗口

o Watch Window

o观察窗

o Quick Watch

o快速观看

o Call Stack

o呼叫堆栈

The following is an explanation of what each button does. (I've changed the order a bit.)

以下是每个按钮的功能说明。 (我已经更改了一下顺序。)

Start/Continue (toggle)

开始/继续(切换)

Availability: Design time and when in break mode

可用性:设计时间和处于中断模式时

Shortcut: F5

捷径:F5

Clicking Start not surprisingly starts your program running just likes F5 does. After Start is clicked it is disabled.

毫不意外地,单击“开始”就像F5一样开始运行程序。 单击开始后,将其禁用。

Break

打破

Availability: Run time only

可用性:仅运行时

Shortcut: CTRL+BREAK

快捷键:CTRL + BREAK

Once your program has started, and, for example, the main form has loaded and it is ready for interaction with the user, pressing the Break button pauses the execution of the program and takes you to the edit window for the form. Pressing the button also has the effect of enabling the Start button and toggling it to Continue. At this point, you can do things like adding or changing code in the Click event of a command button. Pressing the Continue button takes you back to Run mode where you can now click the command button to see the effect of your change. The breaking/changing code/continuing/testing code cycle can, of course, be repeated as many times as needed.

一旦程序启动,例如,主窗体已加载并且可以与用户交互,则按Break按钮将暂停程序的执行,并带您进入该窗体的编辑窗口。 按下按钮还具有启用“开始”按钮并将其切换为“继续”的效果。 此时,您可以执行诸如在命令按钮的Click事件中添加或更改代码的操作。 按“继续”按钮将带您返回“运行”模式,您现在可以单击命令按钮以查看更改的效果。 当然,中断/更改代码/连续/测试代码周期可以根据需要重复多次。

Immediate Window

立即窗口

Availability: At all times but it is read-only at run time

可用性:始终,但在运行时为只读

Shortcut: CTRL+G

快捷键:CTRL + G

The Immediate Window is one of three debugging windows that allow you to monitor the values of expressions and variables while stepping through the statements in your application. The other two windows (which I'll discuss later) are the Watch window and the Locals window.

即时窗口是三个调试窗口之一,可让您在逐步浏览应用程序中的语句时监视表达式和变量的值。 其他两个窗口(稍后将讨论)是“监视”窗口和“本地”窗口。

There are at least four very useful things that you can do with the Immediate Window.

您可以使用“即时窗口”执行至少四项非常有用的操作。

1) Display the results of Debug.Print statements

1) 显示Debug.Print语句的结果

As an example let's assume you have the following code in your command button.

作为示例,假设您在命令按钮中具有以下代码。

Private Sub Command1_Click()

    Dim lngCounter As Long
    
    For lngCounter = 1 To 3
        Debug.Print lngCounter * 20
    Next
    
End Sub 

When you click the command button, the Debug.Print statements will cause the value of lngCounter * 20 to be printed to the Immediate Window so if you then click the Immediate Window button you will see the following.

当您单击命令按钮时,Debug.Print语句将导致将lngCounter * 20的值打印到立即窗口中,因此,如果您再单击立即窗口按钮,您将看到以下内容。

2) Check the value of variables

2) 检查变量的值

While you are in break mode you can display the values of the global variables in your form. For example, if you have a Private intMyInt As Integer statement you can see what the value of intMyInt is by typing ?intMyInt in the Immediate Window and pressing return. The same thing can be accomplished by typing print intMyInt. The values of such things as command button and label captions, textbox text, etc. can be displayed as well. In addition, you can see the value of variables in a Sub or Function if you are at a break point but more about that later.

在中断模式下,可以在表单中显示全局变量的值。 例如,如果您有一个Private intMyInt As Integer语句,则可以通过在立即窗口中键入?intMyInt并按回车键来查看intMyInt的值。 通过键入print intMyInt可以完成相同的操作 。 诸如命令按钮和标签标题,文本框文本等的值也可以显示。 另外,如果您在断点处,则可以在Sub或Function中查看变量的值,但稍后会更多。

3) Change the value of variables and properties

3) 更改变量和属性的值

If you type something like intMyInt = 7 (note that you don't use the question mark) VB will change the value as requested and you can continue execution using that new value. This can be a big time-saver when debugging. A big time-saver when designing a form is that you can, for example, change the value of the Left property of a command button and immediately see what it looks like on the form. If it still needs adjusting you can change it again and again if necessary until you get it right. In this case, you will have to record that value in the IDE, but it is much better than having to repetitively change the value, run the program, see how it looks, stop, change the value, run the program, etc. until you get it right.

如果您键入类似intMyInt = 7的内容 (请注意,您不使用问号),VB将按要求更改该值,您可以使用该新值继续执行。 调试时可以节省大量时间。 设计表单时,最大的节省时间是,例如,您可以更改命令按钮的Left属性的值,然后立即查看它在表单上的外观。 如果仍然需要调整,则可以根据需要一次又一次地更改它,直到正确为止。 在这种情况下,您将必须在IDE中记录该值,但这比重复更改值,运行程序,查看其外观,停止,更改值,运行程序等要好得多。你说对了。

4) Run code

4) 运行代码

It's nice to be able to display the values of variables in the Immediate Window but if you have a set of variables you need to display and you need to do it more than once, retyping them can be tedious. Fortunately, there's a better way. You can simply create a Sub that you run from the Immediate Window! As an example let's assume that you have 3 variables that are involved in your debugging efforts. You would then create a Sub that looks something like this.

能够在“即时窗口”中显示变量的值很不错,但是如果您需要显示一组变量,并且需要执行多次,那么重新键入它们将很繁琐。 幸运的是,有更好的方法。 您可以简单地创建一个从立即窗口运行的Sub! 作为示例,假设您有3个变量参与调试工作。 然后,您将创建一个看起来像这样的Sub。

Private Sub DisplayDebugValues()
    Debug.Print "The value of intMyInt is " & intMyInt
    Debug.Print "The value of lngCounter is " & lngCounter
    Debug.Print "The value of strSomeThing is " & strSomeThing
End Sub 

Once you have that Sub you can display them any time you are in break mode by simply typing the name of the Sub in the Immediate Window, pressing Enter, and the three values will be displayed. Note that in order for this to work, the variables need to be global in scope. In other words, they need to be defined as Public in a module or in the Declarations section of a form or sheet.

一旦有了该Sub,就可以在处于中断模式的任何时间显示它们,只需在立即窗口中键入Sub的名称,然后按Enter,将显示三个值。 请注意,为了使此功能起作用,变量的作用域必须是全局的。 换句话说,需要在模块或表单或工作表的“声明”部分中将它们定义为“公共”。

Toggle Breakpoint

切换断点

Availability: At design time and when in break mode

可用性:在设计时和处于中断模式时

Shortcut: F9

捷径:F9

A breakpoint is a place at which the program will pause until you manually let it resume.  To set a breakpoint you can either place the cursor on the line where you want the code to pause and then press F9 or, more simply, just click in the margin of that line. When you do either of those things VB places a red bullet in the margin and also highlights the line in red. Here is an example where a breakpoint has been placed on a line.

断点是程序暂停的地方,直到您手动让其恢复为止。 要设置断点,您可以将光标放在要暂停代码的行上,然后按F9键,或者更简单地,只需单击该行的空白处。 当您执行上述任一操作时,VB会在空白处放置一个红色的项目符号,并以红色突出显示该行。 这是一个在断点上放置断点的示例。

 

If you then run the program and the breakpoint is reached, VB will highlight the line's code in yellow and then wait there for you to do whatever you want. One of the things that you might want to do is to check the value of variables in the Sub or Function (these are called Local variables) and there are several ways to do that while the code in the Sub or Function is paused. You can hover the mouse over a variable and VB will pop up a little box that shows the variable name and its value, or you can bring up the Immediate Window as we discussed previously, or you can highlight any valid expression and click the Quick Watch window or you can click the Locals Window. The latter two will be described later.

如果然后运行该程序并到达断点,则VB将以黄色突出显示该行的代码,然后在那里等待您执行所需的任何操作。 您可能想做的一件事是检查Sub或Function中的变量的值(这些称为Local变量),并且有几种方法可以在Sub或Function中的代码暂停时执行此操作。 您可以将鼠标悬停在变量上,VB会弹出一个小框,显示变量名称及其值,或者您可以调出前面讨论过的立即窗口,或者可以突出显示任何有效的表达式并单击“快速监视”窗口,也可以单击“本地窗口”。 后两个将在后面描述。

I should mention here that while F9 will toggle a breakpoint on and off, CTRL+SHIFT+F9 will turn all breakpoints off.

我应该在这里提到,虽然F9可以打开和关闭断点,但是CTRL + SHIFT + F9可以关闭所有断点。

Quick Watch

快速观看

Availability: Always available but only useful when in break mode

可用性:始终可用,但仅在中断模式下有用

Shortcut: SHIFT+F9

快捷键:SHIFT + F9

As mentioned previously if you highlight a variable or a valid expression, clicking the Quick Watch button will show you the value. Here's an example where I've done that with txtSquare(intSquare).Text (which is a valid expression) and you can see that the value is Y. Clicking the Add button allows you to add a watch, but more about that later.

如前所述,如果突出显示变量或有效表达式,则单击“快速监视”按钮将为您显示该值。 这是一个使用txtSquare(intSquare).Text(有效表达式)完成此操作的示例,您可以看到该值为Y。单击“添加”按钮可让您添加手表,但稍后将进行更多介绍。

Locals Window

当地人窗口

Availability: Always available but only useful when in break mode

可用性:始终可用,但仅在中断模式下有用

Shortcut: (None)

快捷方式:(无)

Here's what you would see at the same point in the program if you opened the Locals window. I've expanded the information for the form (referred to as Me) and the information for the Controls.Item6.

如果您打开“本地”窗口,则在程序的同一位置会看到以下内容。 我已经扩展了表单信息(称为Me)和Controls.Item6的信息。

Step Into

踏进

Availability: Design time and when in break mode

可用性:设计时间和处于中断模式时

Shortcut: F8

捷径:F8

Step Into allows you to follow the execution of your program line by line as VB executes it. If you press F8 at Design Time the program will start just like it would if you pressed F5 (Start), except that after it is started VB will pause the program just before the first executable line and it will highlight that first executable line in yellow. It will also place a yellow arrow in the left-hand margin. When you press F8 while in break mode VB executes the current line and then pauses at the next line. In either case, you can step through the whole program if you desire by repetitively pressing F8. Note that at any time during that process you can use the Quick Watch, Immediate or Locals window as described above.

单步执行允许您在VB执行程序时逐行跟踪程序的执行情况。 如果在设计时按F8键,则程序将按与按F5键(启动)时一样的方式启动,除了在启动后VB将在第一行可执行文件之前暂停程序,并以黄色突出显示该第一行可执行文件。 。 它还将在左侧边缘放置一个黄色箭头。 在中断模式下按F8键时,VB执行当前行,然后在下一行暂停。 无论哪种情况,都可以重复按F8来逐步浏览整个程序。 请注意,在此过程中,您可以随时使用如上所述的“快速监视”,“立即”或“本地”窗口。

One very useful feature that is available while stepping through the code is the ability to drag the yellow arrow to any executable line of code, thus allowing you to skip or repeat lines.

单步执行代码时可用的一项非常有用的功能是能够将黄色箭头拖动到任何可执行代码行,从而使您可以跳过或重复行。

Step Over

越过

Availability: Design time and when in break mode

可用性:设计时间和处于中断模式时

Shortcut: SHIFT+F8

快捷键:SHIFT + F8

Step Over is similar to Step Into except that as its name implies it will step over or skip the current line. Note that it doesn't skip the execution of the line, only the stepping through. In other words, if the current line is a call to a long Sub that you don't really care about, stepping over will execute the call and then pause at the line following the call.

单步执行与单步执行相似,不同之处在于它的名称暗示它将单步执行或跳过当前行。 请注意,它不会跳过该行的执行 ,只会跳过单步执行 。 换句话说,如果当前行是您实际上并不关心的长Sub调用,则单步执行将执行该调用,然后在该调用之后的行暂停。

Step Out

走出去

Availability: Design time and when in break mode

可用性:设计时间和处于中断模式时

Shortcut: CTRL+SHIFT+F8

快捷键:CTRL + SHIFT + F8

If you are in the same long sub as mentioned above, pressing Step Out will leave that sub and pause at the next executable line. Just like Step Into, Step Out doesn't skip the execution of the rest of the sub, only your viewing of it.

如果与上述相同,则按“跳出”将离开该子目录并在下一个可执行行暂停。 就像“跳入”一样,“跳出”不会跳过其余子项的执行,只会跳过您的查看。

Call Stack

调用堆栈

Availability: In break mode

可用性:处于中断模式

Shortcut: CTRL+L

快捷键:CTRL + L

The Call Stack displays a list of all active procedure calls. An active procedure is the Subs and Functions in the application that were started but not yet completed. In the following trivial example, a breakpoint was placed on last in a string of Subs and the Call Stack shows the order of the calls (bottom-up) that got us to the breakpoint. All the subs are shown in the list because none of them have actually finished yet. If you were to step through the code from this point you would see the subs, starting with Sub5, disappear one by one from the Call Stack as the code progressed back up the chain exiting and thus finishing the subs. Finally, if you double-click on one of the entries or click on it and then press the Show button you will be taken to that procedure and a green triangle will be placed in the left-hand margin showing the next procedure in the stack.

调用堆栈显示所有活动过程调用的列表。 活动过程是应用程序中已启动但尚未完成的Subs and Functions。 在下面的简单示例中,将断点放在一串Subs的最后,并且调用堆栈显示了使我们到达断点的调用顺序(自下而上)。 所有的子项都显示在列表中,因为它们实际上还没有完成。 如果您从这一点开始逐步执​​行代码,您将看到以Sub5开头的子代码,随着代码在链上退出并逐步完成子代码,逐步从调用堆栈中消失了。 最后,如果您双击其中一个条目或单击它,然后按Show(显示)按钮,将进入该过程,并且绿色的三角形将放置在左侧边缘,显示堆栈中的下一个过程。

Watch Window

观察窗

Availability: At all times

可用性:随时

Shortcut: (None)

快捷方式:(无)

We now (finally) come to what I consider the heart of debugging; the Watch Window.

现在,我们(终于)来到了我认为调试的核心。 监视窗口。

Earlier I talked about the Quick Watch window which allows you to see the values of variables or valid expressions when you are at a breakpoint. (Note that from here on when I refer to "expression" that I mean both valid expressions and variables.) The weak point of that method is that you have to set the breakpoint before you are able to use the window. A watch, on the other hand, allows you to set up an expression that VB watches for you as the program runs and optionally pauses the program when a condition you state occurs. In other words, you don't have to know where to put the breakpoint - VB does it for you!

早先,我谈到了“快速监视”窗口,该窗口使您可以在断点时查看变量或有效表达式的值。 (请注意,从这里开始,当我提到“表达式”时,我指的是有效的表达式和变量。)该方法的弱点在于,必须先设置断点, 然后才能使用窗口。 另一方面,手表允许您设置一个表达式,程序在运行时VB会为您监视,并在状态出现时选择暂停程序。 换句话说,您不必知道将断点放在哪里-VB会为您做到这一点!

You can create a watch by

您可以通过以下方式创建手表

o Clicking the Watch Window button, right-clicking on it and choosing Add watch... or

o单击“监视窗口”按钮,右键单击它,然后选择“添加监视...”或

o Clicking the Debug|Add Watch... menu item or

o单击“调试|添加监视...”菜单项,或

o Highlighting an expression, right-clicking, and choosing Add Watch...

o突出显示一个表达式,单击鼠标右键,然后选择“添加监视” ...

When you do one of those things you will be presented with the Add watch window which looks like this.

当您执行其中一项操作时,将显示“添加监视”窗口,如下所示。

Let's now examine the three parts of that window.

现在让我们检查该窗口的三个部分。

1) Expression

1)表达

The Expression is the expression you are interested in. It can be a single variable name like intMyInt or a simple expression like intMyInt = 5 or a complex expression like intMyInt > 5 and SomeOtherVar = 37 and bOK = True. Note that capitalization doesn't count and that while there may be a limit to the complexity or length of the expression, I haven't run across either.

表达式是您感兴趣的表达式。它可以是单个变量名,例如intMyInt ,也可以是简单表达式,例如intMyInt = 5,也可以是复杂表达式,例如intMyInt> 5和SomeOtherVar = 37,并且bOK = True 。 请注意,大写不计算在内,尽管表达式的复杂性或长度可能有限制,但我也没有遇到任何问题。

2) Context

2)上下文

The Context choices defines for VB where you would like it to watch the Expression, and as you see above the default is all procedures in all modules - in other words, the whole program. You will notice I'm sure that the Context choices are two drop-down items and, for example, if you click on the Module drop-down, you will see something like the following where all the modules in the program are listed.

上下文选项为VB定义了您要在其中监视表达式的位置,如您在上面看到的那样,默认值是所有模块中的所有过程-换句话说,整个程序。 您会注意到我确定Context选项是两个下拉项,例如,如果单击Module下拉列表,则会看到类似以下内容的内容,其中列出了程序中的所有模块。

If you have selected a module then clicking on Procedure will display the procedures contained in that module.

如果您选择了一个模块,则单击“过程”将显示该模块中包含的过程。

2) Watch Type

2)手表类型

There are three Watch Types and you can choose one of them.

共有三种手表类型,您可以选择其中一种。



Watch Expression 手表表情

In order to use Watch Expression effectively, you need to have a breakpoint set. Once the watch is created and the breakpoint is reached, clicking on the Watch window button will display something like the following. Note that the Watch Window is dock-able and once docked there's no need to click on it in order for its contents to be seen.

为了有效地使用Watch Expression,您需要设置一个断点。 创建手表并到达断点后,单击“手表”窗口按钮将显示类似以下内容。 请注意,“监视窗口”是可停靠的,一旦停靠,就无需单击它即可看到其内容。







The eyeglasses symbol under Expression tells you that it's a Watch Expression, and the little box with the plus sign indicates that txtSquare has more than one property that we can look at. Clicking on the plus sign will expand the result to show you all the properties and their values. Note that if the Watch Expression had been txtSquare(Index).Text instead of just txtSquare(Index) that there would be no plus sign since we were specific about the property we were interested in. The other things displayed in the widow are pretty self-explanatory but I'll mention them for completeness. Value is the value of the expression, Type is the type of item you are looking at, and Context is the scope of the Watch Expression.

Expression下的眼镜符号告诉您这是一个Watch Expression,带有加号的小框表示txtSquare具有多个可以查看的属性。 单击加号将展开结果,以向您显示所有属性及其值。 请注意,如果Watch Expression是txtSquare(Index).Text而不是txtSquare(Index),则由于我们对所关注的属性是特定的,因此不会有加号。寡妇中显示的其他内容都是很简单的-不言而喻,但为了完整起见,我会提及它们。 值是表达式的值,类型是您要查看的项的类型,上下文是监视表达式的范围。



This is probably as good a place as any to mention that once you have a Watch Expression you can change or delete any watch by way of the Debug|Edit Watch menu item.

这可能和任何提及的地方一样好,一旦您有了Watch Expression,您就可以通过Debug | Edit Watch菜单项来更改或删除任何手表。





Break When value Is True 值为真时中断







Break When Value Changes 价值变化时打破

As far as I know, there is no limit to the number of watches you can have and I could add a third watch with mstrLastPlayer as the Expression and Break When Value Changes selected. That type of watch is indicated in the window by a yellow-triangle-in-hand symbol and I'm sure you have already figured out that that watch would pause the program any time mstrLastPlayer changed value.

据我所知,您可以拥有的手表数量没有限制,我可以添加第三个带有mstrLastPlayer的手表作为“表达式”和“值更改时中断”。 这种类型的手表在窗口中用黄色三角符号指示,我相信您已经知道,只要mstrLastPlayer更改值,该手表就会暂停程序。

Unfortunately watches only exist as long as the particular project is open and can not be saved. Also, a watch that you are no longer interested in can get in your way by, for example, continually pausing the program when one of the "Break" type watches becomes true or its value changes. You can, of course, get around that by deleting the particular watch or you can simply edit the watch by adding say, an "X" at the front of the expression thereby making it invalid. Once it is invalid VB will ignore it but not delete it. Later on, if you like you can remove the "X" which will reactivate it.

不幸的是,只有特定项目处于打开状态且无法保存时,监视才存在。 另外,您不再感兴趣的手表可能会遇到麻烦,例如,当“ Break”类型的手表之一变为真或其值发生更改时,会不断暂停程序。 当然,您可以通过删除特定手表来解决该问题,也可以通过在表达式的前面添加“ X”来简单地编辑手表,从而使其无效。 一旦无效,VB将忽略它,但不会删除它。 以后,如果您愿意,可以删除将重新激活它的“ X”。

Debug Is A Great Tool

调试是一个很棒的工具

Over the years I've found that Debug has been an invaluable asset in helping me track down the bugs that slip into my programs, and I hope that this tutorial has given you enough of an understanding of it so that you will feel comfortable using it to track down your errors.

多年来,我发现Debug是帮助我追踪程序中的错误的宝贵资产,我希望本教程对您有足够的了解,以便您可以轻松使用它跟踪您的错误。

To give you a start at using it I've attached a version of a Tic-Tac-Toe game I wrote that has a bug. What happens is that the second time either "X" or "O" wins a game their total shows as 3 rather than 2. You may well be able to find the bug by just visually inspecting the program but I encourage you to try to find it using the tools provided by Debug.

为了让您开始使用它,我附加了我编写的带有错误的Tic-Tac-Toe游戏的一个版本。 发生的情况是“ X”或“ O”第二次赢得一场比赛时,他们的总成绩为3而不是2。您可以通过直观地检查程序来找到错误,但我鼓励您尝试查找使用Debug提供的工具。

Tic-Tac-Toe-Bug.zip

Tic-Tac-Toe-Bug.zip

If you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!

如果您发现本文对您有所帮助,请单击下面的“竖起大拇指”按钮。 这样做可以让我知道对EE成员有价值的内容,并为以后的文章提供指导。 它还以几点的形式为我提供了积极的反馈。 谢谢!

翻译自: https://www.experts-exchange.com/articles/8631/Debugging-VBA-and-VB6-Applications.html

vba程序的vb6封装

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值