【问题标题】:Not able to read property at runtime无法在运行时读取属性
【发布时间】:2014-07-13 13:45:54
【问题描述】:

当我试图读取控件的"Left property" 时,它给了我错误,

"Left cannot be read at run time"

这是我的代码,

for each ctrl in me.controls
    if ctrl.left > 2490 then
        'app logic
    end if
next

这段代码有什么问题。它在另一台计算机上正常工作。 谁能告诉我怎么回事

【问题讨论】:

    标签: vb6


    【解决方案1】:

    您的表单上可能只有设计时可放置的控件,例如timer,它没有运行时左属性。您可以检查控件的类型以确保仅检查TextBoxLabelButton 等,或者只使用on error resume 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
    

    使用On Error Resume 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
    

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-18
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        • 2018-06-28
        • 2021-04-07
        相关资源
        最近更新 更多