否则如果不能在 Visual Basic 6 中工作

else if not working in visual basic 6

我想在图片框中显示选中的图片。以下是代码:

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

Private Sub File1_Click()
On Error GoTo lab
lab:
If Err.Number = 481 Then
    MsgBox ("Please select a valid Picture")
Else
    If Err.Number = 68 Then
        MsgBox ("Device not ready")
    End If
End If
Resume Next
Picture1.Picture = LoadPicture(File1.Path + "\" + File1.FileName)
End Sub

案例 481 工作得很好,但第二种情况,错误 68 根本不起作用。它显示 运行 时间错误 68。以下是输出:

请让我知道上面代码中的错误。

您通常将错误处理程序 放在可能引发错误的代码之前 吗?我无法用我自己的简单案例复制它,但这段代码的结构似乎很可疑。您还有一个没有循环的 Resume Next 。试试这个:

Private Sub File1_Click()
On Error GoTo lab
Picture1.Picture = LoadPicture(File1.Path + "\" + File1.FileName)
Exit Sub

lab:
Select Case Err.Number
    Case 481 Then
        MsgBox ("Please select a valid Picture")
    Case 68 Then
        MsgBox ("Device not ready")
End Select

End Sub

听起来您可能需要在其他过程中使用额外的错误处理程序,例如:

Private Sub Dir1_Change()
    On Error Resume Next
    File1.Path = Dir1.Path
    If Err.Number = 0 Then Exit Sub
    Select Case Err.Number
        Case 68
            Msgbox ("Device not ready")
        Case Else
            MsgBox ("Error " & Err.NUmber) '# Modify as needed...
    End Select
End Sub

Private Sub Drive1_Change()
    On Error Resume Next
    Dir1.Path = Drive1.Drive
    If Err.Number = 0 Then Exit Sub
    Select Case Err.Number
        Case 68
            Msgbox ("Device not ready")
        Case Else
            MsgBox ("Error " & Err.NUmber) '# Modify as needed...
    End Select
End Sub