vb.net - 在VB中添加错误处理程序

标签 vb.net error-handling

  ' Button which allows the user to enter in there own password
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click


        Try
            If TextBox3.Text & TextBox6.Text & TextBox7.Text & TextBox8.Text > 0 And TextBox4.Text <> "" Then
                'input in these textboxes must be numerical
                ' It adds the passwords which is made by the user himself 
                ListBox1.Items.Add("Application : " & TextBox4.Text & " Password : " & TextBox3.Text & TextBox6.Text & TextBox7.Text & TextBox8.Text & " Time : " & TimeString & " Date : " & DateTime.Today)

            End If
        Catch ex As Exception
            MessageBox.Show("Please provide correct input")


        End Try

我目前正在Visual Basic上制作一个密码生成器,我已经在用户可以自己设置密码的部分中显示了代码,但是我希望有一些错误处理程序,目前我可以使用它,因此显示错误消息如果将非数字信息输入到textbox3,6,7,8中。这可行,但是我想补充两点
1.如果textboxes3,6,7,8为空,则会显示一条错误消息
2.如果textbox4为空,则显示一条错误消息,当前列表框中未打印任何内容,但未显示任何错误消息。

压力
vb的初学者

最佳答案

您实际上并不需要异常处理来进行验证。您可以使用内置方法来执行此操作。

例如,以下代码检查名为ExampleTextBox的texbox是否具有输入,并且输入是有效整数。

 If String.IsNullOrEmpty(ExampleTextBox.Text) Then
   'no input
 Else
   'has input, check if number
    Dim enteredNumber As Integer

    If Integer.TryParse(ExampleTextBox.Text, enteredNumber) Then
       'input is a valid integer
    Else
       'input is not a valid integer
    End If

End If

关于vb.net - 在VB中添加错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673883/

相关文章:

Vb.net - 在我的标签内居中文本!

python - 永远运行python脚本,无需担心错误

php - 即使session_start()已经在最前面,“session_start(): Cannot send session cache limiter - headers already sent”仍会继续出现

php - 为什么不显示wordpress fatal error ?

mysql - 选择 localhost 的语句并插入网络数据库

vb.net - For Each 语句中是否可以有两个元素?

vb.net - New 语句后内联调用实例方法

python - 为返回 api 的 json 自定义 flask 错误处理程序

python - 在Python中使用os.rename()时出现FileNotFoundError

vb.net - Visual Studio/VB.Net 2008 IntelliSense 奇怪行为