0

我试图做到这一点,所以当我的按钮被点击时,它将运行一系列任务或要做的事情,但我想要它,所以如果它无法完成这些任务中的任何事情,它会(做某事) - 喜欢使用MsgBox("此操作无法完成...")

这是我的按钮代码:

Private Sub SelectBtn_Click(sender As Object, e As EventArgs) Handles SelectBtn.Click
    Dim res = client.Get("Logins/" + usernamebox.Text)
    Dim std As New Student()
    std = res.ResultAs(Of Student)
    If std.Password = passwordbox.Text Then
        MsgBox("Welcome Back! " + usernamebox.Text + "!")
    Else
        MsgBox("Username OR Password May Be Wrong", MsgBoxStyle.Exclamation, "Login Error")
    End If

End Sub
    

好的,这是我的按钮代码,它从 Firebase 获取数据.. 但我想要它,所以当它失败时它不会破坏程序.. 而是显示一条消息......我想要这个的原因是出于某种原因当一个值不“存在”时程序会中断它会中断,我只想显示一条消息,如果它试图中断并继续再次单击它等等。对不起我输入的方式我非常习惯于 Visual Studio 2019 ......事情的格式真的把我搞砸了......

编辑 : 这就是我在使用 Try 块时得到的结果

4

1 回答 1

0

您将需要使用 Try/Catch 块:https ://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement

在您的情况下,它可能看起来像这样:

Try
    Dim res = client.Get("Logins/" + usernamebox.Text)
    Dim std As New Student()
    std = res.ResultAs(Of Student)

    ' student is null, login failed
    If (std Is Nothing) Then
        MessageBox("Username OR Password May Be Wrong", MsgBoxStyle.Exclamation, "Login Error")
    End If

    If (std.Password = passwordbox.Text) Then
        MessageBox("Welcome Back! " + usernamebox.Text + "!")
    Else
        ' student is not null, but the password doesn't match
        MessageBox("Username OR Password May Be Wrong", MsgBoxStyle.Exclamation, "Login Error")
    End If
Catch ex As Exception
    MessageBox.Show("This operation could not be completed.")
    Console.WriteLine(ex.Message) ' see what cause the exception in the output dialog
End Try
于 2021-01-07T17:33:53.207 回答