2

通过重构一些旧的 VB6 代码,我偶然发现了一些非常奇怪的行为。当我尝试设置控件数组元素的 DragMode 属性时,当我将此控件与其数组分开时,编译器会告诉我“找不到方法或数据成员”。

请参阅下面的示例,其中“myControl”只是一个带有文本框的用户控件。'Controls' 是一个包含 myControls 的控件数组。第一个循环有效,第二个循环无效。

Dim i As Integer
Dim ctrl As myControl

For i = 0 To 2
    myControls(i).DragMode = vbAutomatic
Next i

For Each ctrl In myControls
    ctrl.DragMode = vbAutomatic
Next

更新:

感谢@wqw,我让 for each 循环正常工作。对我有用的代码现在看起来像这样:

Dim ctrlExt As VBControlExtender
Dim ctrl As myControl

For Each ctrlExt In myControls

    Set ctrl = ctrlExt

    ctrlExt.DragMode = vbAutomatic
    ctrl.SpecificProperty = "Test"
Next
4

1 回答 1

2

尝试Dim ctrl As VBControlExtender

这有效

Dim i As Integer
Dim ctrl As VBControlExtender

For Each ctrl In Controls
    ctrl.DragMode = vbAutomatic
Next

For i = MyControls.LBound To MyControls.UBound
    MyControls(i).DragMode = vbAutomatic
Next i
于 2013-02-14T14:57:40.970 回答