Vb6 无法在运行时读取属性

Vb6 无法在运行时读取属性,vb6,Vb6,当我试图读取控件的“Left属性”时,它给出了错误 “运行时无法读取左侧” 这是我的密码 for each ctrl in me.controls if ctrl.left > 2490 then 'app logic end if next 此代码中有什么错误。它在另一台计算机上正常工作。 有谁能告诉我出了什么问题吗?您的表单上可能有一个仅设计时可放置的控件,例如,计时器,它没有运行时left属性。您可以检查控件的类型,以确保只有文本框、标签、按钮等。请

当我试图读取控件的
“Left属性”
时,它给出了错误

“运行时无法读取左侧”

这是我的密码

for each ctrl in me.controls
    if ctrl.left > 2490 then
        'app logic
    end if
next
此代码中有什么错误。它在另一台计算机上正常工作。
有谁能告诉我出了什么问题吗?

您的表单上可能有一个仅设计时可放置的控件,例如,
计时器
,它没有运行时left属性。您可以检查控件的类型,以确保只有
文本框
标签
按钮等。请选中,或者在下一步错误恢复时使用

Dim ctrl As Control

On Error Resume Next
for each ctrl in me.controls
    if ctrl.left > 2490 then
        'app logic
    end if
Next
使用
TypeOf
检查对象类型:

Dim ctrl As Control

For Each ctrl In Me.Controls
    If TypeOf ctrl Is Timer Then
    Else
        If ctrl.Left > 2490 Then
            'app logic
        End If
    End If
Next
使用
TypeName
检查对象类型:

Dim ctrl As Control

For Each ctrl In Me.Controls
    If TypeName(ctrl) = "Timer" Then
    Else
        If ctrl.Left > 2490 Then
            'app logic
        End If
    End If
Next
在错误时使用
继续下一步

Dim ctrl As Control

On Error Resume Next
for each ctrl in me.controls
    if ctrl.left > 2490 then
        'app logic
    end if
Next

如果使用最后一种方法,则处理内联错误并重新引发任何意外错误非常重要。否则,如果你得到的错误与你期望的不同,你可能很难找到它。因此:

Dim ctrl As Control

On Error Resume Next
for each ctrl in me.controls
    if ctrl.left > 2490 then
        Select Case Err.Number
           Case 0        'No Error, ignore
           Case 393      'The error you want to ignore 
              Err.Clear  'Reset for next iteration
           Case Else
              On Error Goto 0
              Err.Raise Err.Number  'Reraise any unexpected errors
        End Select
        'app logic
    end if
Next
On Error Goto 0