在 catch 块中设置文本框 属性 值
Setting a textbox property value within a catch block
对我的 ClickOnce 应用程序使用 VS 2013 VB.net。我有一个验证数据库功能的函数,它的内容被包裹在 Try Catch
中。我的 Catch 块的一部分如下所示:
Catch ex As Exception When Err.Number = "5"
My.Application.Log.WriteException(ex)
If My.Settings.g_blnDebugMode Then
MessageBox.Show(Err.Number & " " & ex.ToString, "Exception Error")
End If
If Err.Description.Contains("The specified table does not exist") Then
MessageBox.Show("Selected file is not a valid database.", "Exception Error")
ElseIf Err.Description.Contains("The specified password does not match the database password.") Then
MessageBox.Show("The specified password does not match the current database password.", "Exception Error")
End If
Return False
我想做的是,根据底部的两个自定义错误消息清除两个不同的字段。 TextBox1.Text = ""
或 TextBox2.Text = ""
之类的东西,具体取决于抛出的错误(无效密码或无效数据库)。我的问题是我似乎无法直接设置它们或从 catch 块中设置模块或全局变量的值。
错误是:
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
如果可能,我该如何解决这个问题并根据 Catch 块中的结果设置我的文本框?
通常实现您所尝试的方法是使用第二个 catch 块
Catch ex As Exception When Err.Number = ""
MessageBox.Show(ex.Message)
FlushTextBox1();
Catch ex As Exception When Err.Number = ""
MessageBox.Show(ex.Message)
FlushTextBox2();
可能会出现此错误,因为 try-catch 块位于 Shared
方法内。这意味着 TextBox
的值的变化将对 class 的所有实例重复。如果您希望 TextBox
表现得像这样,请将 Shared
关键字添加到它自己的声明中并将其从 Sub 的声明中删除。如果您根本不想要这种行为,只需删除 Shared
关键字即可。有关错误的更多信息,请查看 MSDN article
或者,您可以调用局部函数(如代码所示)FlushTextBox1()
来更改 Sub.TextBox
外的值。
对我的 ClickOnce 应用程序使用 VS 2013 VB.net。我有一个验证数据库功能的函数,它的内容被包裹在 Try Catch
中。我的 Catch 块的一部分如下所示:
Catch ex As Exception When Err.Number = "5"
My.Application.Log.WriteException(ex)
If My.Settings.g_blnDebugMode Then
MessageBox.Show(Err.Number & " " & ex.ToString, "Exception Error")
End If
If Err.Description.Contains("The specified table does not exist") Then
MessageBox.Show("Selected file is not a valid database.", "Exception Error")
ElseIf Err.Description.Contains("The specified password does not match the database password.") Then
MessageBox.Show("The specified password does not match the current database password.", "Exception Error")
End If
Return False
我想做的是,根据底部的两个自定义错误消息清除两个不同的字段。 TextBox1.Text = ""
或 TextBox2.Text = ""
之类的东西,具体取决于抛出的错误(无效密码或无效数据库)。我的问题是我似乎无法直接设置它们或从 catch 块中设置模块或全局变量的值。
错误是:
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
如果可能,我该如何解决这个问题并根据 Catch 块中的结果设置我的文本框?
通常实现您所尝试的方法是使用第二个 catch 块
Catch ex As Exception When Err.Number = ""
MessageBox.Show(ex.Message)
FlushTextBox1();
Catch ex As Exception When Err.Number = ""
MessageBox.Show(ex.Message)
FlushTextBox2();
可能会出现此错误,因为 try-catch 块位于 Shared
方法内。这意味着 TextBox
的值的变化将对 class 的所有实例重复。如果您希望 TextBox
表现得像这样,请将 Shared
关键字添加到它自己的声明中并将其从 Sub 的声明中删除。如果您根本不想要这种行为,只需删除 Shared
关键字即可。有关错误的更多信息,请查看 MSDN article
或者,您可以调用局部函数(如代码所示)FlushTextBox1()
来更改 Sub.TextBox
外的值。